在尝试从popover显示UIActionSheet时,是否有人收到此消息?
您的应用程序提供了样式UIAlertControllerStyleActionSheet的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息。您必须提供sourceView和sourceRect或barButtonItem。如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它。
以前通用GM我使用了一些解决方法将UIActionSheet转换为UIAlertController,这很好用。 然而,似乎Apple试图解决UIActionSheet问题,我不想使用我的解决方法 - 但似乎我别无选择......
答案 0 :(得分:102)
要支持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)
答案 1 :(得分: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];
答案 2 :(得分:7)
您需要提供popoverPresentationController
iPad支持。在此,您可以指定barButtonItem
或sourceView
。另一个主题可以帮助您:Swift UIAlertController - ActionSheet iPad iOS8 Crashes
答案 3 :(得分:2)
实际上,对于iPhone和iPad设计来说,它在Xcode中是一种错误(我相信)。
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纵向设计的近似尺寸差异。UIAlertController
附带了“模态视图”'但不幸的是,如果你在iPad上使用相同的代码,它将不会是一个模态视图'。这意味着您需要编写额外的代码来禁用iPad设计中的触摸。我认为这很奇怪。这些是我看到的奇怪的事情。我认为必须有一个标准,如果有人想要出标准,那么可以有其他选择。
答案 4 :(得分:1)
UIAlertController仅限iOS8,需要支持iOS7,我正在使用它。我在iPad上的主/细节布局中的主视图上遇到了这个问题。通过使用[actionSheet showInView:]从父UISplitViewController引发UIActionSheet,我能够解决它(不完全修复它)。祝你好运。
答案 5 :(得分:0)
如果要在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)