在iOS 8.0中,苹果引入了UIAlertController来取代UIActionSheet。不幸的是,苹果没有提供任何关于如何展示的信息。我在hayaGeek的博客上找到了一个关于它的条目,然而,它似乎不能在iPad上运行。这种观点是完全错误的:

错误的:

正确的:

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

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

是否有其他方法添加到iPad上?或者苹果只是忘记了iPad,或者还没有推出iPad ?


当前回答

斯威夫特5

我在iPhone上使用“actionsheet”样式,在iPad上使用“alert”样式。iPad显示在屏幕中央。不需要指定sourceView或将视图锚定在任何位置。

var alertStyle = UIAlertController.Style.actionSheet
if (UIDevice.current.userInterfaceIdiom == .pad) {
  alertStyle = UIAlertController.Style.alert
}

let alertController = UIAlertController(title: "Your title", message: nil, preferredStyle: alertStyle)

编辑:根据ShareToD的建议,更新了已弃用的“UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.”垫“检查

其他回答

2018年更新

我刚刚因为这个原因拒绝了一款应用,而我的一个快速解决方法便是将操作表单改为提醒。

很有魅力,顺利通过了App Store测试。

可能不是每个人都适合这个答案,但我希望这能帮助你们中的一些人迅速摆脱困境。

这里有一个快速的解决方案:

NSString *text = self.contentTextView.text;
NSArray *items = @[text];

UIActivityViewController *activity = [[UIActivityViewController alloc]
                                      initWithActivityItems:items
                                      applicationActivities:nil];

activity.excludedActivityTypes = @[UIActivityTypePostToWeibo];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    //activity.popoverPresentationController.sourceView = shareButtonBarItem;

    activity.popoverPresentationController.barButtonItem = shareButtonBarItem;

    [self presentViewController:activity animated:YES completion:nil];

}
[self presentViewController:activity animated:YES completion:nil];

斯威夫特5

适用于iPad和iPhone。

动作表下方悬停一个按钮

对于iPhone,动作表将显示正常,iPad将显示在按钮下方。

actionSheet.popoverPresentationController?.sourceView = fooButton
actionSheet.popoverPresentationController?.sourceRect = fooButton.bounds

动作表在屏幕/视图的中间

对于iPhone,动作表将正常显示,iPad将显示在屏幕/视图的中间。

actionSheet.popoverPresentationController?.sourceView = view
actionSheet.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
actionSheet.popoverPresentationController?.permittedArrowDirections = []

它将同时适用于iphone和ipad

func showImagePicker() {
    var alertStyle = UIAlertController.Style.actionSheet
    if (UIDevice.current.userInterfaceIdiom == .pad) {
      alertStyle = UIAlertController.Style.alert
    }
    let alert = UIAlertController(title: "", message: "Upload Attachment", preferredStyle: alertStyle)
    alert.addAction(UIAlertAction(title: "Choose from gallery", style: .default , handler:{ (UIAlertAction) in
        self.pickPhoto(sourceType: .photoLibrary)
    }))
    alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler:{ (UIAlertAction) in
        self.pickPhoto(sourceType: .camera)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction) in
    }))
    present(alert, animated: true, completion: nil)
}

斯威夫特4.2 你可以像这样使用condition:

let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet)