2

I have a navigation bar that contains multiple buttons, including a back button, edit button and a button that opens a popover.

When the popover is open and the user taps any of the other buttons, I want the popover to close. I could try to detect every tap on all the buttons and intercept the action if the popover is open but I though there might be a more elegant option. It works with taps on all items that are not inside the navbar, only button in the navigation bar don't dismiss the popover.

Any suggestions?

My question is very similar to UIPopoverController does not dismiss when clicking on the NavigationBar but I don't seem to have a way to ask the author if he solved the problem.

Community
  • 1
  • 1
Stefan Henze
  • 2,711
  • 23
  • 22

3 Answers3

10

I know this might be a bit late but for everyone else:

The reason this problem occurs is that the navigation controller is by default present in the passthroughViews of the popoverController if the popoverController is presented on click of button present on the navigation bar .

To solve this just make the reference of the passthroughViews to nil.

So just after presenting the popoverController add this line :

popoverController.passthroughViews = nil;

Hopefully this helps someone.

Chris W. Rea
  • 5,430
  • 41
  • 58
Ashwani
  • 1,574
  • 14
  • 28
  • 2
    Yes, but if the user rotates the iPad, the bar buttons will respond to taps when the popover is still displayed. I tried to set the passthroughViews to nil again in didRotateFromInterfaceOrientation but that doesn't work. UIKit insists on re-displaying the popovers AFTER didRotateFromInterfaceOrientation is called... – Raymond Law May 14 '13 at 04:52
  • In my application it works to dismiss the popover when the display orientation changes. (see also: http://stackoverflow.com/questions/14170404/willrotatetointerfaceorientation-not-being-called-from-presented-viewcontroller). – Chris Prince Jan 28 '14 at 02:19
2

Assign the selector of each button to the same method, first of all, check if the popover is open, then close it then redirect each button to it's method.

-(IBAction) navButtons:(UIBarButtonItem *)sender {
     if(![popoverController isPopoverVisible] && sender.tag == 1){//assume that just one button will open the popover
         //present the popover
     } else {
         //dismiss the popover
     }

     switch (sender.tag) {
        case 1:
             [self button1Handler];
             break;
        case 2:
             [self button2Handler];
             break;
        /*...
          ...
          ...*/
        default:
             break;
    }
}

I think this is the best solution you could use.

Scar
  • 3,460
  • 3
  • 26
  • 51
  • Thank you very much, I was hoping for a way to have the navbar not respond to the events at all. I will go this route for now then, thank you! – Stefan Henze Oct 07 '12 at 23:34
0

tems 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.

Please see following link for code sample:

https://stackoverflow.com/a/27054252/2919070

Community
  • 1
  • 1
hufeng03
  • 404
  • 5
  • 9