ios - 使用 iOS 8 在 iPad 上正确呈现 UIAlertController

标签 ios ipad user-interface uialertcontroller

在 iOS 8.0 中,Apple 推出了 UIAlertController替换 UIActionSheet .不幸的是,Apple 没有添加任何关于如何呈现它的信息。我找到了一个 entry hayaGeek 的博客上有关于它的介绍,但是它似乎在 iPad 上不起作用。该 View 完全放错了位置:

错位: Misplaced image

正确: enter image description here

我用下面的代码在界面上显示:

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

还有其他方法可以为 iPad 添加它吗?还是 Apple 只是忘记了 iPad,或者尚未实现?

最佳答案

您可以使用 UIPopoverPresentationController 从弹出窗口显示 UIAlertController

在 Obj-C 中:

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

针对 Swift 4.2 进行编辑,虽然有很多博客可用于相同的内容,但它可以节省您搜索它们的时间。

if let popoverController = yourAlert.popoverPresentationController {
    popoverController.sourceView = self.view //to set the source of your alert
    popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) // you can set this as per your requirement.
    popoverController.permittedArrowDirections = [] //to hide the arrow of any particular direction
}

关于ios - 使用 iOS 8 在 iPad 上正确呈现 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224916/

相关文章:

iphone - iOS 手势问题 - 如何检测持续触摸特定时间间隔的触摸事件?

ios - 实现持久性倒数计时器

java - 自定义 JTabbedPane

java - 创建新对象时 JFrame 不显示组件

java - 是否建议使用 ProgressDialog 进行登录?

ios - 从 UICollectionViewCell 传播自定义事件

ios - iOS返回的图像大小不同?

ios - 初始比例是否忽略固定的视口(viewport)宽度?

iphone - 透明UIImage

javascript - 使用 jQTouch javascript iphone/ipad 的简单滑动画廊