UIPopoverController does not dismiss when clicking on the NavigationBar

怎甘沉沦 提交于 2019-11-27 07:35:45

问题


When clicking on a rightBarButton, a UIPopoverController will present.

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

Why? And is there a way to resolve it? I have tried to search, but can't find anything about this.

Thanks in advance.


回答1:


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.




回答2:


When launching from a bar button you can simply do this

[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverController setPassthroughViews:nil];



回答3:


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
    }
}



回答4:


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.




回答5:


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];



回答6:


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.



来源:https://stackoverflow.com/questions/6136792/uipopovercontroller-does-not-dismiss-when-clicking-on-the-navigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!