UIActionSheet not showing in iOS 8

2019-02-23 11:11发布

问题:

I can show the actionSheet in iOS 7 but cannot show in iOS 8 environment. Is there any way to trace?

  UIActionSheet *_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Account"
                                                               delegate:self 
                                                      cancelButtonTitle:@"Close" 
                                                 destructiveButtonTitle:@"Log Out" 
                                                      otherButtonTitles:@"View Account", nil];

    [_actionSheet showInView:appDel.window];

回答1:

UIActionSheet is deprecated in iOS 8.

To create and manage action sheets in iOS 8 you should use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet.

Refer this example.



回答2:

UIAlertController * alert=   [UIAlertController
                             alertControllerWithTitle:@"Info"
                             message:@"You are using UIAlertController"
                             preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
                    actionWithTitle:@"OK"
                    style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction * action)
                    {
                        [alert dismissViewControllerAnimated:YES completion:nil];

                    }];
UIAlertAction* cancel = [UIAlertAction
                        actionWithTitle:@"Cancel"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [alert dismissViewControllerAnimated:YES completion:nil];

                       }];

 [alert addAction:ok];
 [alert addAction:cancel];

 [self presentViewController:alert animated:YES completion:nil];


回答3:

It's clearly a regression in iOS 8. I have filed a rdar, please do so, too.

In the meantime, you can fix it with a category or a subclass where you check whether the view is a window, and if so, then grab the rootViewController's view on that window and use it instead. It'll work that way.



回答4:

I had issues with action sheets when iOS 8 came out. It is likely an issue with the view you are presenting in. Can you show it in an actual view rather than the window? I'd experiment there first, and as a worst case, wrap the show method up in something specific for iOS 7 and 8. I started using PSTAlertController to manage my alerts and action sheets. It supports UIAlertView, UIActionSheet, and UIAlertController. It will sort out which one to use, as well as give you access to block actions for your buttons while still supporting iOS 7 and 8. Worth a look.



回答5:

If anyone else sees this issue, the problem is actually caused by iOS 8 and 9 displaying your action sheet behind the onscreen keyboard, so you can't actually see it. You just see the rest of the screen going darker. Genius.

The simple solution is to add one line of code before displaying your UIActionSheet:

[self.view endEditing:true];

This dismisses the onscreen keyboard, making your beautiful action sheet visible again.