How to make UIPopoverController keep same position after rotating?

前端 未结 13 1147
粉色の甜心
粉色の甜心 2021-01-30 18:21

I can\'t keep popover the same position on the screen after rotation. Is there any good way to do that, because just setting some frame to popover works terrible after rotating.

13条回答
  •  春和景丽
    2021-01-30 18:55

    Swift 3:

        class MyClass: UIViewController, UIPopoverPresentationControllerDelegate {
    
    
            ...
    
            var popover:UIPopoverPresentationController?
    
            ...
    
            // Where you want to set the popover...
            popover = YourViewController?.popoverPresentationController
            popover?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            popover?.delegate = self
    
            ...
    
            // override didRotate...
            override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
              popover?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            }
    
    }
    

提交回复
热议问题