ios 来自 Popover 的 UIActionSheet 和 iOS8 GM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25759885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIActionSheet from Popover with iOS8 GM
提问by Tomer Peled
Anyone is getting this message while trying to show UIActionSheet from popover?
有人在尝试从弹出窗口显示 UIActionSheet 时收到此消息吗?
Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
您的应用程序呈现了一个 UIAlertControllerStyleActionSheet 样式的 UIAlertController ()。具有此样式的 UIAlertController 的 modalPresentationStyle 是 UIModalPresentationPopover。您必须通过警报控制器的 popoverPresentationController 提供此弹出框的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果您在呈现警报控制器时不知道此信息,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。
Previously to the GM I used some workaround for converting the UIActionSheet to UIAlertController and this is working fine. However it seems that Apple tried to solve the UIActionSheet issues and I didn't want to use my workaround - but it seems that I have no choice...
在 GM 之前,我使用了一些解决方法将 UIActionSheet 转换为 UIAlertController,这工作正常。然而,Apple 似乎试图解决 UIActionSheet 问题,我不想使用我的解决方法 - 但似乎我别无选择......
回答by Philip Jang
To support iPad, include this code:
要支持 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)
回答by Stunner
If you are presenting the action sheet after the user makes a selection on a cell within a UITableView
. I found that this works decently well:
如果您在用户在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];
回答by Aнгел
You need to provide popoverPresentationController
for iPad support. In this, you either specify barButtonItem
or sourceView
. This another thread may help you: Swift UIAlertController - ActionSheet iPad iOS8 Crashes
您需要提供popoverPresentationController
对 iPad 的支持。在此,您可以指定barButtonItem
或sourceView
。这个另一个线程可以帮助你:Swift UIAlertController - ActionSheet iPad iOS8 Crashes
回答by icould
Actually it is something buggy (I believe) in Xcode for iPhone and iPad designs for now.
实际上,目前 iPhone 和 iPad 设计的 Xcode 有问题(我相信)。
- In iPhone same code works perfect and you can see the alert message at same position (always). But for iPad you need to define the alert box's position with
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 and 70 are the approximate dimension differences for iPad portrait design due to different anchor point. - In iPhone design
UIAlertController
comes with 'Modal View' but unfortunately if you use same code for iPad it will not be a 'Modal View'. Which means that you need to write extra code for disabling touches in iPad design. I think it is weird. - In iPad design you need to consider that anchor point is different. It is the bubble triangle point, not the upper left of AlertView.
- 在 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 纵向设计的近似尺寸差异。 - 在 iPhone 设计
UIAlertController
中带有“模态视图”,但不幸的是,如果您对 iPad 使用相同的代码,它将不是“模态视图”。这意味着您需要编写额外的代码来禁用 iPad 设计中的触摸。我觉得这很奇怪。 - 在 iPad 设计中,你需要考虑锚点是不同的。它是气泡三角点,而不是 AlertView 的左上角。
These are the weird things I see. I think that there must be a standard and if someone wants to go out standards, fine, there can be other options.
这些是我看到的奇怪的东西。我认为必须有一个标准,如果有人想走出标准,那很好,可以有其他选择。
回答by Mohit Singh
If you want to present it in the centre with no arrows on iPads and normally on iPhones: [Swift 3+]:
如果您想在 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)
回答by Adam Hyman
UIAlertController being iOS8 only, and needing to support iOS7, I am using it. I ran into this on a Master view in a Master/Detail layout on iPad. I was able to work around it (not exactly fix it) by raising the UIActionSheet from the parent UISplitViewController using [actionSheet showInView:]. Good luck.
UIAlertController 仅适用于 iOS8,并且需要支持 iOS7,我正在使用它。我在 iPad 上的 Master/Detail 布局中的 Master 视图中遇到了这个问题。我能够通过使用 [actionSheet showInView:] 从父 UISplitViewController 提升 UIActionSheet 来解决它(不完全修复它)。祝你好运。