弹出不指向按钮
Pop over doesn't point over the button
我有一个与 iPhone 和 iPad 布局兼容的应用程序。对于 iPhone 布局,我创建了 Action Sheet 并为 iPad 创建了 Pop over。问题是弹出箭头没有指向我单击的按钮。下面是我的代码....
let actionSheet = UIAlertController(title: "Choose an option",
message: "Message",
preferredStyle: .ActionSheet)
...
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
{
// for iPad
actionSheet.popoverPresentationController?.sourceView = self.view
actionSheet.popoverPresentationController?.sourceRect = self.view.bounds;
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
}
self.presentViewController(actionSheet, animated: true, completion: nil)
设置sourceView
和sourceRect
为button
和button.bounds
。
您可以根据视图的布局选择 permittedArrowDirections。
actionSheet.popoverPresentationController?.sourceView = button
actionSheet.popoverPresentationController?.sourceRect = button.bounds;
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Left;
如果按钮是 BarButtonItem,请使用此代码。
actionSheet.popoverPresentationController?.barButtonItem = button
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up;
对我来说,使用发件人并投射为 UIView。
alertController.popoverPresentationController?.sourceView = sender as! UIView
alertController.popoverPresentationController?.sourceRect = sender.bounds
alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
SWIFT 3
当我的按钮是 UIBarButtonItem 时,这对我有用:
if UIDevice.current.userInterfaceIdiom == .pad {
if controller.responds(to: "popoverPresentationController") {
controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
}
}
下面的完整代码片段:
func presentActivitySheet() {
let controller = UIActivityViewController(activityItems: [document.fileURL], applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
if controller.responds(to: "popoverPresentationController") {
controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
}
}
present(controller, animated: true, completion: nil)
}