在iOS 8.0中,苹果推出了UIAlertController以取代UIActionSheet.不幸的是,苹果没有添加任何关于如何展示它的信息.我在哈耶克的博客上找到了一个关于它的entry,然而,它在iPad上似乎不起作用.这种观点完全是错误的:

放错位置: 放错位置的图像

更正: 在此处输入图像描述

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

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

有没有其他方法可以把它添加到iPad上?或者苹果只是忘记了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,虽然有很多博客可以提供相同的内容,但go 搜索它们可能会节省您的时间.

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相关问答推荐

如何调整包含extView的UICollectionViewListCell的大小?

为什么Reaction-Native-Vision-Camera不能与闪光灯一起使用?

从后台线程调用DispatchObject的方法安全吗?

如何在iOS 17中拦截UITextView中的链接点击?UITextItemInteraction已弃用

在flutter中使用flatter_screenutil这样的软件包的目的是什么?

SwiftUI-工作表不显示导航标题

保存和读取登录到 keys 串不工作 IOS swift

在 Swift 中,对象如何被准确地从内存中移除?

Apple Metal iOS 为什么 GLKMatrix4MakeLookAt 来回摇摆

SwiftUI:如何用拇指移动滑块值

如何在 Swift 3 中根据 UIImage 的大小/比率调整 UIImageView 的大小?

Select UITableViewCell 时 UIView backgroundColor 消失

通过touch 传递到下面的 UIViews

如何使用 sendAsynchronousRequest:queue:completionHandler:

在 iOS 7 上更改标签栏色调 colored颜色

UITextView 从文本的底部或中间开始

iOS中的apk类似功能是什么?

关闭通过模态 segue 显示的视图

在 Objective-C 中将所有文本转换为小写

iOS 中的 Google Analytics SDK 3.0 _sqlite3 链接器错误