Pop over doesn't point over the button

别等时光非礼了梦想. 提交于 2019-12-03 01:19:52

Set the sourceView and sourceRect as the button and button.bounds.
You can choose the permittedArrowDirections depending on the layout of your view.

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

If the button is a BarButtonItem use this code.

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

For me worked using the sender and casting as UIView.

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

alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up

SWIFT 3

This worked for me when my button was a UIBarButtonItem:

if UIDevice.current.userInterfaceIdiom == .pad {

    if controller.responds(to: "popoverPresentationController") {
        controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
    }

}

Entire code snippet below:

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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!