When clicking on a rightBarButton, a UIPopoverController will present.
The problem is: when clicking on the NavigationBar, this UIPopoverController does not dismiss
When launching from a bar button you can simply do this
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverController setPassthroughViews:nil];
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.
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
}
}
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.
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];
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.