弹出窗口没有指向按钮

时间:2015-01-07 15:49:39

标签: ios ipad swift popover

我的应用程序兼容iPhone和iPad布局。对于iPhone布局,我创建了Action Sheet和Pop over iPad。问题是弹出箭头没有指向我点击的按钮。以下是我的代码......

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)

3 个答案:

答案 0 :(得分:91)

sourceViewsourceRect设置为buttonbutton.bounds
您可以根据视图的布局选择allowedArrowDirections。

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

如果按钮是BarButtonItem,请使用此代码。

actionSheet.popoverPresentationController?.barButtonItem = button
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up;

答案 1 :(得分:2)

对我来说,使用发件人和铸造为UIView。

alertController.popoverPresentationController?.sourceView = sender as! UIView
alertController.popoverPresentationController?.sourceRect = sender.bounds

alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up

答案 2 :(得分:1)

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)
}