UIActionSheet not showing in iOS 8

让人想犯罪 __ 提交于 2019-12-01 21:08:34

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.

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];

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!