How to make UIPopoverController keep same position after rotating?

前端 未结 13 1126
粉色の甜心
粉色の甜心 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:51

    1. Initialize PopOver Controller

      var popoverContent: PopoverContentViewController?
      
    2. Write Defination for PopOver Controller

      popoverContent = self.storyboard?.instantiateViewController(withIdentifier: "PopoverContentViewController") as? PopoverContentViewController
      popoverContent?.modalPresentationStyle = .popover
      let popover = popoverContent?.popoverPresentationController!
      popover?.delegate = self
      popoverContent?.preQuestionInfoPopUpViewDelegateObject = self
      popover?.permittedArrowDirections = UIPopoverArrowDirection()
      popover?.sourceView = self.view
      popover?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 330, height: 330)
      
    3. Present PopOver Controller

      self.present(popoverContent, animated: true, completion:nil)

    4. Write below method and assign new size to popover:

      override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { let popover = popoverContent?.popoverPresentationController! popover?.sourceRect = CGRect(x: size.width/2, y: size.height/2, width: 0, height: 0) }

提交回复
热议问题