Add UIPickerView in UIActionSheet from IOS 8 not working
Asked Answered
C

6

51

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

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)];
    }
}
Catheryncatheter answered 23/6, 2014 at 12:51 Comment(1)
maybe it would be helpful: #11629613Beerbohm
P
21

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];
Propriety answered 16/9, 2014 at 10:30 Comment(5)
grrr, did they have to break the old? Seems this is the new direction using actions and alertStyle.Plascencia
did you try this? addSubview does not seem to work with UIAlertController.Plascencia
I tried a different direction, which led to a new question. Perhaps you have some insight. #25976474Plascencia
I get a EXC_BAD_ACCESS when using addSubviewCassady
Can I see your code and the exception line . And do you deploy on iOS 8 .Propriety
P
20

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:.

Proserpina answered 23/6, 2014 at 13:7 Comment(2)
But presentViewController only displays a full screen view controller modally - how do we make it look similar to a UIActionsheet?Periclean
You are totally right. It looks like Apple now make sure that no one can abuse this component. I've debug the UIActionSheet and currently it is a fake UIView and plays a role of proxy object. It doesn't have any subviews and doesn't have a superview drawRect: and layoutSubviews is never called even when it is visible. So any visual manipulation on UIActionSheet will always fail and there is no simple workaround for it. Same issue applies for UIAlertView.Octant
J
8

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];
}
Jago answered 29/9, 2014 at 8:25 Comment(0)
C
0

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.

Caban answered 23/9, 2014 at 9:50 Comment(5)
Deprecated does not mean deleted. UIActionSheet works fine in iOS 8Cruse
Actually, they don't work properly in iOS8+. If you have the onscreen keyboard visible, then the keyboard will appear on top of your UIActionSheet, so you won't be able to see your action sheet options !! You'll just see the screen go dark.Progressionist
@MikeGledhill you need send resignFirstResponder message to your keyboard before actionsheet will be shownCaban
I tried that, it didn't work. The keyboard just stayed on the screen. What DID work was adding this line of code: #26062464Progressionist
@MikeGledhill thanks, I got you, I check it later bro)Caban
K
-2
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
                                              cancelButtonTitle:nil 
                                         destructiveButtonTitle:nil 
                                              otherButtonTitles:nil];
Kearney answered 25/9, 2014 at 7:30 Comment(0)
F
-4

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)];
Fagoting answered 23/6, 2014 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.