how to center a popoverview in swift

前端 未结 10 1743
终归单人心
终归单人心 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:12

    Here's an implementation using Swift 3

    let popover = storyboard?.instantiateViewController(withIdentifier: "popover") as! PopoverVC
    
        popover.modalPresentationStyle = UIModalPresentationStyle.popover
        popover.popoverPresentationController?.backgroundColor = UIColor.green
        popover.popoverPresentationController?.delegate = self
    
        popover.popoverPresentationController?.sourceView = self.view
        popover.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
    
        popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
    
        self.present(popover, animated: true)
    

    Based on Istvan's answer

提交回复
热议问题