在 IOS 8 的 UIActionSheet 中添加 UIPickerView 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24366437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:03:32  来源:igfitidea点击:

Add UIPickerView in UIActionSheet from IOS 8 not working

iosiphoneuipickerviewuiactionsheetios8

提问by Jignesh Patel

I am adding UIPickerViewto UIActionsheetbut its working perfectally in ios 7but not working in ios 8. Please help me to solve that.

我正在添加UIPickerViewUIActionsheet但它ios 7ios 8. 请帮我解决这个问题。

Here i attached screenshot for that.enter image description here

在这里,我附上了截图。在此处输入图片说明

Thanks

谢谢

I am using following code for that.

我为此使用了以下代码。

UIActionSheet *actionSheet;
NSString *pickerType;

- (void)createActionSheet {

    if (actionSheet == nil) {
        // setup actionsheet to contain the UIPicker
        actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please select" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        actionSheet.backgroundColor = [UIColor clearColor];

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];

        UIImageView *imageObj = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        imageObj.image = [UIImage imageNamed:@"header.png"];
        [pickerToolbar addSubview:imageObj];

        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn addTarget:self action:@selector(pickerCancel:)forControlEvents:UIControlEventTouchDown];
        // [cancelBtn setImage:[UIImage imageNamed:@"btnInviteCancel.png"] forState:UIControlStateNormal];
        [cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];

        cancelBtn.frame = CGRectMake(7.0, 7.0, 65.0, 25.0);
        [pickerToolbar addSubview:cancelBtn];

        UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [doneBtn addTarget:self action:@selector(pickerDone:)forControlEvents:UIControlEventTouchDown];
        //[doneBtn setImage:[UIImage imageNamed:@"btnInviteDone.png"] forState:UIControlStateNormal];
        [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
        doneBtn.frame = CGRectMake(258.0, 7.0, 55.0, 25.0);
        [pickerToolbar addSubview:doneBtn];

        [actionSheet addSubview:pickerToolbar];
        [pickerToolbar release];


        UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 200.0)];
        chPicker.dataSource = self;
        chPicker.delegate = self;
        chPicker.showsSelectionIndicator = YES;
        [chPicker selectRow:0 inComponent:0 animated:YES];
        [actionSheet addSubview:chPicker];
        [chPicker release];

        [actionSheet showInView:self.view];
        [actionSheet setBounds:CGRectMake(0,0,320, 464)];
    }
}

回答by Arek Holko

It doesn't work because Apple changed internal implementation of UIActionSheet. Please refer to the documentation:

它不起作用,因为 Apple 更改了UIActionSheet. 请参考文档

Subclassing Notes

UIActionSheet is not designed to be subclassed, norshould you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

子类化注释

UIActionSheet 不是设计为子类化的,也不应该向其层次结构添加视图。如果您需要呈现比 UIActionSheet API 提供的自定义更多的表单,您可以创建自己的表单并使用presentViewController:animated:completion: 以模态方式呈现它。

回答by Nada Gamal

Per the Apple docs,

根据Apple 文档

Important: UIActionSheetis deprecated in iOS 8. (Note that UIActionSheetDelegateis also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertControllerwith a preferredStyleof UIAlertControllerStyleActionSheet.

重要:UIActionSheet在 iOS 8 中UIActionSheetDelegate已弃用。(请注意,它也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请使用UIAlertControllera preferredStyleof UIAlertControllerStyleActionSheet

Link to documentation for UIAlertController

链接到 UIAlertController 的文档

For example:

例如:

     UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

                [ searchActionSheet.view setBounds:CGRectMake(7, 180, self.view.frame.size.width, 470)];

                //yourView represent the view that contains UIPickerView and toolbar
                [searchActionSheet.view addSubview:self.yourView];
                [self presentViewController:searchActionSheet animated:YES completion:nil];

回答by skywinder

There is out-of-the-box ready solution with exactly the same behaviour - ActionSheetPicker-3.0

有开箱即用的解决方案,具有完全相同的行为 - ActionSheetPicker-3.0

Original version used UIActionsheet, but with iOS 8 release I upgrade this project and replace UIActionsheet with simple view, but animation and view looks exactly the same.

使用原始版本UIActionsheet,但在 iOS 8 版本中我升级了这个项目并用简单的视图替换了 UIActionsheet,但动画和视图看起来完全一样。

animation

动画片

ActionSheetPicker-3.0

ActionSheetPicker-3.0

回答by ki.bowox

I found this problem too and I have a solution, I hope it can help you..

我也发现了这个问题,已经解决了,希望能帮到你。。

You can download the sample code here: https://www.dropbox.com/s/yrzq3hg66pjwjwn/UIPickerViewForIos8.zip?dl=0

您可以在此处下载示例代码:https: //www.dropbox.com/s/yrzq3hg66pjwjwn/UIPickerViewForIos8.zip?dl=0

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
    UIView *maskView;
    UIPickerView *_providerPickerView;
    UIToolbar *_providerToolbar;
}


- (void) createPickerView {
    maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [maskView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];

    [self.view addSubview:maskView];
    _providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 344, self.view.bounds.size.width, 44)];

    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet:)];
    _providerToolbar.items = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], done];
    _providerToolbar.barStyle = UIBarStyleBlackOpaque;
    [self.view addSubview:_providerToolbar];

    _providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 300, 0, 0)];
    _providerPickerView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
    _providerPickerView.showsSelectionIndicator = YES;
    _providerPickerView.dataSource = self;
    _providerPickerView.delegate = self;

    [self.view addSubview:_providerPickerView];
}

- (void)dismissActionSheet:(id)sender{
    [maskView removeFromSuperview];
    [_providerPickerView removeFromSuperview];
    [_providerToolbar removeFromSuperview];
}

回答by Mudriy Ilya

That doesn't show anything because uiactionsheet & uiactionsheetdelegate is deprecated is ios8 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html

这没有显示任何内容,因为不推荐使用 uiactionsheet 和 uiactionsheetdelegate 是 ios8 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html

You need to use UIAlertController in ios 8.

您需要在 ios 8 中使用 UIAlertController。

回答by user3651677

actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
                                              cancelButtonTitle:nil 
                                         destructiveButtonTitle:nil 
                                              otherButtonTitles:nil];

回答by Satish Azad

Try this when presenting UIActionSheet in last 2 lines:

在最后两行显示 UIActionSheet 时试试这个:

[actionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];