UIPopoverPresentationController displaying popover as full screen

前端 未结 6 2042
小蘑菇
小蘑菇 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:41

    The accepted answer is correct. For completeness, see Adapting Presented View Controllers to a New Style in the Apple docs:

    Use the delegate’s adaptivePresentationStyleForPresentationController: method to specify a different presentation style than the default. When transitioning to a compact environment, the only supported styles are the two full-screen styles or UIModalPresentationNone. Returning UIModalPresentationNone tells the presentation controller to ignore the compact environment and continue using the previous presentation style. In the case of a popover, ignoring the change gives you the same iPad-like popover behavior on all devices.

    Make sure that the required configurations from Presenting a View Controller in a Popover are met:

    After setting the modal presentation style [of the presented view controller] to UIModalPresentationPopover, configure the following popover-related attributes:

    • Set the preferredContentSize property of your view controller to the desired size.
    • Set the popover anchor point using the associated UIPopoverPresentationController object, which is accessible from the view controller’s popoverPresentationController property.
    • Set only one of the following:
      • Set the barButtonItem property to a bar button item.
      • Set the sourceView and sourceRect properties to a specific region in one of your views.
    0 讨论(0)
  • 2020-12-24 12:52

    For Swift3/IOS10, looks like we need to do some thing like

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

    Adding this answer, in case, someone runs into this problem as i did when migrating to swift3/IOS10

    0 讨论(0)
  • 2020-12-24 12:53

    In iPhone, you should add the following in order to present a popover.

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
        // Return no adaptive presentation style, use default presentation behaviour
        return .None
    }
    
    0 讨论(0)
  • 2020-12-24 12:55

    iOS 12 / Swift 4

    There's also the possibility to show the popover on IPhone's as fullscreen and on IPad's as a popover.

    Just return .popover for adaptivePresentationStyle():

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .popover
    }
    

    and set the popover-configuration like @mourodrigo did:

    dialog.modalPresentationStyle = .popover
    dialog.popoverPresentationController?.delegate = self
    dialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
    dialog.popoverPresentationController?.sourceView = view
    dialog.popoverPresentationController?.sourceRect = view.frame
    
    0 讨论(0)
  • 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
       }
    
     }
    
    0 讨论(0)
  • 2020-12-24 13:08

    objective-c version:

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationNone;
    }
    
    0 讨论(0)
提交回复
热议问题