使用iOS 8在iPad上正确呈现UIAlertController [英] Presenting a UIAlertController properly on an iPad using iOS 8

查看:490
本文介绍了使用iOS 8在iPad上正确呈现UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8.0中,Apple推出了 UIAlertController 替换 UIActionSheet 。不幸的是,Apple没有添加任何有关如何呈现它的信息。我在hayaGeek的博客上发现了一个关于它的条目,但它似乎无法工作iPad兼容。视图完全错位:

With iOS 8.0, Apple introduced UIAlertController to replace UIActionSheet. Unfortunately, Apple didn't add any information on how to present it. I found an entry about it on hayaGeek's blog, however, it doesn't seem to work on iPad. The view is totally misplaced:

错位:

正确:

我使用以下代码在界面上显示它:

I use the following code to show it on the interface:

    let alert = UIAlertController()
    // setting buttons
    self.presentModalViewController(alert, animated: true)

有没有其他方法可以为iPad添加它?或者苹果只是忘了iPad,还是没有实现?

Is there another way to add it for iPad? Or did Apple just forget the iPad, or not implemented, yet?

推荐答案

你可以提出 UIAlertController 来自popover,使用 UIPopoverPresentationController

You can present a UIAlertController from a popover by using UIPopoverPresentationController.

UIViewController *self; // code assumes you're in a view controller
UIButton *button; // the button you want to show the popup sheet from

UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;

alertController = [UIAlertController alertControllerWithTitle:nil
                                                      message:nil
                           preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
                                         style:UIAlertActionStyleDestructive
                                       handler:^(UIAlertAction *action) {
                                           // do destructive stuff here
                                       }];
otherAction = [UIAlertAction actionWithTitle:@"Blah"
                                       style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction *action) {
                                         // do something here
                                     }];
// note: you can control the order buttons are shown, unlike UIActionSheet
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];

UIPopoverPresentationController *popPresenter = [alertController 
                                              popoverPresentationController];
popPresenter.sourceView = button;
popPresenter.sourceRect = button.bounds;
[self presentViewController:alertController animated:YES completion:nil];

这篇关于使用iOS 8在iPad上正确呈现UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆