Add UIPickerView in UIActionSheet from IOS 8 not working

前端 未结 6 2086
时光说笑
时光说笑 2020-11-30 23:37

I am adding UIPickerView to UIActionsheet but its working perfectally in ios 7 but not working in ios 8. Please help me t

相关标签:
6条回答
  • 2020-12-01 00:00

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

    Subclassing Notes

    UIActionSheet is not designed to be subclassed, nor should 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:.

    0 讨论(0)
  • 2020-12-01 00:01
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
                                                  cancelButtonTitle:nil 
                                             destructiveButtonTitle:nil 
                                                  otherButtonTitles:nil];
    
    0 讨论(0)
  • 2020-12-01 00:02

    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

    @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];
    }
    
    0 讨论(0)
  • 2020-12-01 00:10

    Try this when presenting UIActionSheet in last 2 lines:

    [actionSheet showFromRect:CGRectMake(0,480, 320,215) inView:self.view animated:YES];
    [actionSheet setBounds:CGRectMake(0,0,320, 464)];
    
    0 讨论(0)
  • 2020-12-01 00:11

    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

    You need to use UIAlertController in ios 8.

    0 讨论(0)
  • 2020-12-01 00:16

    Per the Apple docs,

    Important: UIActionSheet is deprecated in iOS 8. (Note that UIActionSheetDelegate is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.

    Link to documentation for 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];
    
    0 讨论(0)
提交回复
热议问题