UIPopoverPresentationController displaying popover as full screen

前端 未结 6 2074
小蘑菇
小蘑菇 2020-12-24 12:24

I am trying to use UIPopoverPresentationController to display a popover that doesn\'t take up the whole screen. I\'ve followed many different tutor

6条回答
  •  余生分开走
    2020-12-24 12:59

    For Swift3+/IOS10+, when dealing with iPhone:

    You must add UIPopoverPresentationControllerDelegate the delegate at:

    class YourClass:  UIViewController, UIPopoverPresentationControllerDelegate { ...
    

    Then implement in this same parent class (which will show the popover) the method below.

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
    {
        return .none
    }
    

    And then set the popover configuration below:

    myPopover.modalPresentationStyle = .popover
    myPopover.popoverPresentationController?.sourceRect = VIEWTOPOINTTHEARROW.frame
    myPopover.popoverPresentationController?.sourceView = self.view
    myPopover.popoverPresentationController?.delegate = self
    

    Also you may set some configuration for the popover class

     class MyPopover: UIViewController {
    
     override func viewDidLoad() {
        super.viewDidLoad()
        //popover size
        self.preferredContentSize = CGSize(width: 320, height: 200) 
        //sets the arrow of the popover to same color of background
        self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
       }
    
     }
    

提交回复
热议问题