Pop over不会指向button

我有一个兼容iPhone和iPad布局的应用程序。 对于iPhone布局,我已经为iPad创build了操作表和popup窗口。 问题是popup的箭头不指向我点击的button。 以下是我的代码….

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) 

sourceViewsourceRect设置为buttonbutton.bounds
您可以根据视图的布局来selectallowedArrowDirections。

 actionSheet.popoverPresentationController?.sourceView = button actionSheet.popoverPresentationController?.sourceRect = button.bounds; actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Left; 

如果该button是一个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

当我的button是一个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) }