从IOS 8在UIActionSheet中添加UIPickerView无法正常工作

Jig*_*tel 52 iphone uipickerview uiactionsheet ios ios8

我正在补充UIPickerView,UIActionsheet但它的工作完美,ios 7但没有工作ios 8.请帮我解决这个问题.

在这里我附上了截图.在此输入图像描述

谢谢

我正在使用以下代码.

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)];
    }
}
Run Code Online (Sandbox Code Playgroud)

Are*_*lko 21

它不起作用,因为Apple改变了内部实施UIActionSheet.请参阅文档:

子类注释

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

  • 你完全正确.看起来苹果现在确保没有人可以滥用这个组件.我调试了`UIActionSheet`,目前它是一个假的`UIView`并扮演代理对象的角色.它没有任何`subviews`并且没有`superview``drawRect:`和`layoutSubviews`从未被调用,即使它是可见的.因此,对"UIActionSheet"的任何视觉操作都将始终失败,并且没有简单的解决方法.同样的问题适用于`UIAlertView`. (2认同)

Nad*_*mal 21

根据Apple文档,

重要提示:UIActionSheet在iOS的8已被弃用(请注意,UIActionSheetDelegate也已经过时.)创建和iOS 8的管理动作片,后来,改用UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet.

链接到UIAlertController的文档

例如:

     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];
Run Code Online (Sandbox Code Playgroud)

  • grrr,他们有没有打破旧的?似乎这是使用actions和alertStyle的新方向. (2认同)

sky*_*der 11

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

使用原始版本UIActionsheet,但在iOS 8版本中,我升级此项目并用简单视图替换UIActionsheet,但动画和视图看起来完全相同.

动画

ActionSheetPicker-3.0


小智 8

我也发现了这个问题,我有一个解决方案,希望它可以帮到你..

您可以在此处下载示例代码: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];
}
Run Code Online (Sandbox Code Playgroud)