Problems with action sheets in iOS 9

Apple is deprecated in the action sheet in iOS 8.3, how do you add an action sheet to your user interface?

I realized that Apple's documentation is not entirely clear on how to create an action sheet using the UIAlertController. So after a little game, I just wanted to share my code, since I could not find anything useful in the Stack Exchange about this topic.

+7
ios9 uialertcontroller
Sep 28 '15 at 13:24
source share
3 answers

I had the same problem in my iPhone app, with UIActionSheet , to ask the user if they want to take a picture or select an image from their gallery.

enter image description here

Prior to iOS 9, the following code worked fine:

 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take photo", @"Choose Existing", nil]; [actionSheet showInView:self.view]; 

However, in iOS 9 all this makes the screen completely darker and nothing appears.

Yes ... thanks Apple.

The solution is to replace the UIActionSheet with a UIAlertController .

 UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* button0 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { // UIAlertController will automatically dismiss the view }]; UIAlertAction* button1 = [UIAlertAction actionWithTitle:@"Take photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // The user tapped on "Take a photo" UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:^{}]; }]; UIAlertAction* button2 = [UIAlertAction actionWithTitle:@"Choose Existing" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // The user tapped on "Choose existing" UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:^{}]; }]; [alert addAction:button0]; [alert addAction:button1]; [alert addAction:button2]; [self presentViewController:alert animated:YES completion:nil]; 

Please note that if you did not enable the undo option (an option with the UIAlertActionStyleCancel style), an action sheet will appear, but clicking anywhere outside the action sheet will not be canceled.

One of them:

Despite the fact that we indicated that we want this UIAlertController have the UIAlertControllerStyleActionSheet style, you need to set the title to nil , not an empty line, otherwise you will get an ugly space at the top of the window.

enter image description here

I can't wait to find out which iOS 9.2 code works great ...

Update

My comment: “However, in iOS 9 it all does, the screen darkens completely and nothing appears” was a bit wrong.

In fact, I opened my UIActionSheet while the on-screen keyboard was visible. And in iOS 9, the keyboard will appear on top of your UIActionSheet , so you can no longer see your action sheet (!!), but you will see that the rest of the screen has turned darker.

With UIAlertController iOS is a little more convenient as it hides the on-screen keyboard before trying to display the action screen at the bottom of the screen. That is why Apple is not doing the same with UIActionSheets outside of me.

(Sigh.)

Please, can I return to using Visual Studio, now ..?

Another update

Apple said UIActionSheets are now "deprecated in iOS 8."

However, if you want to use them on iOS screens containing text fields, then the workaround for this error is errr, the problem is to add one line of code before displaying the UIActionSheet :

 [self.view endEditing:true]; 

This allows you to disable the on-screen keyboard before displaying an "obsolete" action sheet.

(Sigh.) If someone needs me, I will be in the pub.

+18
Feb 10 '16 at 13:00
source share

Here is a snippet of code that I used in my applications for an action sheet, just in case someone needs help trying to figure out how to use the UIAlertController as an action sheet.

 UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:@"This is an action sheet." preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Button Title 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //code to run once button is pressed }]; UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Button Title 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //code to run once button is pressed }]; [alert addAction:button1]; [alert addAction:button2]; [self presentViewController:alert animated:YES completion:nil]; 
+5
Sep 28 '15 at 13:24
source share

In iOS 9 and iPad, I had to add some important changes. Maybe someone in the future may need this.

  UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Server" message:@"Choose server to point to" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Development" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //code to run once button is pressed }]; UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Production" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //code to run once button is pressed }]; [alert addAction:button1]; [alert addAction:button2]; UIPopoverPresentationController *popPresenter = [alert popoverPresentationController]; popPresenter.sourceView = self.view; //To present the actionsheet the bottom of screen popPresenter.sourceRect = CGRectMake(self.createBtn.frame.origin.x, self.view.frame.size.height, self.createBtn.frame.size.width, self.createBtn.frame.size.height); alert.modalPresentationStyle = UIModalPresentationPopover; [self presentViewController:alert animated:YES completion:nil]; 
+1
Nov 21 '16 at 8:54
source share



All Articles