iOS8 GM中如何从弹出窗口显示UIActionSheet

47

当您尝试从弹出窗口显示UIActionSheet时,是否收到此消息?

您的应用程序已经展示了一个UIAlertController(),样式为UIAlertControllerStyleActionSheet。使用此样式的UIAlertController的modalPresentationStyle为UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController提供此弹出窗口的位置信息。 您必须提供sourceView和sourceRect或barButtonItem中的任何一项。如果在呈现警报控制器时不知道此信息,则可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它。

之前我使用了一些解决方案将UIActionSheet转换为UIAlertController,在GM版本之前这个解决方案运行得很好。 但看起来苹果试图解决UIActionSheet的问题,我不想使用我的解决方案 - 但似乎我别无选择...


你的意思是UIAcitonsheet已经被弃用了,但你仍然想使用它吗? - Jageen
2
它已被弃用,但仍应该能够工作... - Tomer Peled
6个回答

106

为了支持iPad,请加入以下代码:

alertView.popoverPresentationController?.sourceView = self.view
alertView.popoverPresentationController?.sourceRect = self.view.bounds
// this is the center of the screen currently but it can be any point in the view

self.presentViewController(alertView, animated: true, completion: nil)

2
shareMenu 是什么?它是 alertVc 吗? - Misha
1
@Misha,是的。这应该是alertVC。 - Robert Altman

11

如果用户在UITableView中选择单元格后再显示操作表,我发现以下方法效果还不错:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions" 
                                                               message:@"Select mode of transportation:"
                                                        preferredStyle:UIAlertControllerStyleActionSheet];
alert.popoverPresentationController.sourceView = cell;
alert.popoverPresentationController.sourceRect = cell.bounds;
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
//...
[self presentViewController:alert animated:YES completion:nil];

7

2

实际上,目前在Xcode的iPhone和iPad设计中存在一些错误(我相信是这样的)。

  1. 在iPhone上,相同的代码可以完美运行,并且您可以在同一位置(始终如一)看到警报消息。但是在iPad上,您需要使用alert.popoverPresentationController.sourceView = self.view; alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0 - 105, self.view.bounds.size.height / 2.0 + 70, 1.0, 1.0);定义警报框的位置。105和70是iPad纵向设计中由于不同的锚点而产生的近似尺寸差异。
  2. 在iPhone设计中,UIAlertController带有“模态视图”,但不幸的是,如果您在iPad上使用相同的代码,则不会是“模态视图”。这意味着您需要编写额外的代码来禁用iPad设计中的触摸功能。我认为这很奇怪。
  3. 在iPad设计中,您需要考虑锚点的不同之处。它是气泡三角形的点,而不是AlertView左上角。

这些是我看到的奇怪事情。我认为必须有一个标准,如果有人想要超出标准,那么可以有其他选项。


1

由于UIAlertController只支持iOS8及以上版本,但我的要求是需要支持iOS7及以上版本。在iPad的Master/Detail布局中,我遇到了这个问题。最终我通过在父级UISplitViewController中使用[actionSheet showInView:]来调用UIActionSheet来解决了这个问题(并不完全修复)。祝好运。


1
如果您想在iPad上居中显示且没有箭头,在iPhone上通常显示:[Swift 3+]:
if let popoverController = alertController.popoverPresentationController {
    popoverController.sourceView = self.view
    popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
    popoverController.permittedArrowDirections = []
}
self.present(alertController, animated: true, completion: nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接