how to center a popoverview in swift

前端 未结 10 1737
终归单人心
终归单人心 2020-12-04 16:42

I have the following code to show a popoverview (dialog) without an arrow, which works fine. The only problem is, that the dialog is shown in the top left (IPad). I would li

10条回答
  •  星月不相逢
    2020-12-04 17:08

    Another way for Swift 3 (Xcode 8, iOS 9) is this:

    Called from somewhere:

    self.performSegue(withIdentifier: "showPopupSegue", sender: yourButton)
    

    Function that gets called before segue gets fired:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let popoverPresentationController = segue.destination.popoverPresentationController {
            let controller = popoverPresentationController
            controller.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
            controller.sourceView = self.view
            controller.sourceRect = CGRect(x: UIScreen.main.bounds.width * 0.5 - 200, y: UIScreen.main.bounds.height * 0.5 - 100, width: 400, height: 200)
            segue.destination.preferredContentSize=CGSize(width: 400, height: 200)
        }
    }
    

    Remember to set the storyboard segue's Kind attribute to "Present as Popover" and Anchor attribute to any view in your previous view controller.

提交回复
热议问题