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

编程入门 行业动态 更新时间:2024-10-28 09:27:17
本文介绍了使用 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?

推荐答案

您可以使用 UIPopoverPresentationController 从弹出窗口中呈现 UIAlertController.

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

针对 Swift 4.2 进行编辑,虽然有很多相同的博客可用,但它可能会节省您去搜索它们的时间.

Editing for Swift 4.2, though there are many blogs available for the same but it may save your time to go and search for them.

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 8 在 iPad 上正确呈现 UIAlertController

本文发布于:2023-11-27 19:29:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   iOS   iPad   UIAlertController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!