UIPopoverController does not dismiss when clicking on the NavigationBar

后端 未结 6 1923
耶瑟儿~
耶瑟儿~ 2020-12-11 15:10

When clicking on a rightBarButton, a UIPopoverController will present.

The problem is: when clicking on the NavigationBar, this UIPopoverController does not dismiss

相关标签:
6条回答
  • 2020-12-11 15:19

    When launching from a bar button you can simply do this

    [popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    [popoverController setPassthroughViews:nil];
    
    0 讨论(0)
  • 2020-12-11 15:19

    The documentation for UIPopoverController states:

    When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.

    The navigation bar is added as one of the passthroughViews when the popover is presented from a bar button item.

    Perhaps try settings an empty array as the passthroughViews property on your popover controller.

    0 讨论(0)
  • Items on your navigation bar will be automatically added to popoverViewController's passthroughViews. It happens after the popover shows up. So you need to clear passthroughViews after that.

    And for iOS 8, we can get popoverController from UIViewController.popoverPresentationController, before that, we can get popoverController from UIStoryboardPopoverSegue.

    In your view controller presents a view controller as popover.

    var popoverController: UIPopoverController?
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Before IOS8, we need to get reference of popOverController from UIStoryboardPopoverSegue
        if (!self.respondsToSelector(Selector("popoverPresentationController"))) {
            if let popoverController = (segue as? UIStoryboardPopoverSegue)?.popoverController {
                let menuViewController = segue.destinationViewController as AIMSMenuTableViewController
                menuViewController.popoverController = popoverController
            }
        }
    }
    

    In your view controller that is presented as popover.

    var popoverController: UIPopoverController? 
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // Set passthroughViews to nil make tapping other navigation bar button
        // dismiss presenting popoverController
        if (self.respondsToSelector(Selector("popoverPresentationController"))) {
            self.popoverPresentationController?.passthroughViews = nil
        } else {
            // For iOS8-pre version, we need to pass popoverController reference from segue
            self.popoverController?.passthroughViews = nil
        }
    }
    
    0 讨论(0)
  • 2020-12-11 15:27

    This is expected behavior as far as I can tell. The popover on the bookshelf in iBooks behaves like this. Retain a reference to the popover when you present it, and then dismiss it if any of the buttons in the navigation bar are tapped.

    0 讨论(0)
  • 2020-12-11 15:30

    You put this cod on any other action or After completing the selection or provide some close button in the popover and accomplish uy yhing,

    [popOverControllerObj dismissPopoverAnimated:YES];
    
    0 讨论(0)
  • 2020-12-11 15:38

    UIPopoverController seems to add the navigation bar to its passthroughViews array when it is presented. I was able to solve the problem by re-setting passthroughViews to an empty array immediately after presenting the popover.

    0 讨论(0)
提交回复
热议问题