Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a UINavigationController's UIToolbar during viewWillDisappear:

I've got an iPhone application with a UITableView menu. When a row in the table is selected, the appropriate view controller is pushed onto the application's UINavigationController stack.

My issue is that the MenuViewController does not need a toolbar, but the UIViewControllers which are pushed onto the stack do. Each UIViewController that gets pushed calls setToolbarHidden:animated: in viewDidAppear:. To hide the toolbar, I call setToolbarHidden:animated: in viewWillDisappear:.

Showing the toolbar works, such that when the pushed view appears the toolbar slides up and the view resizes correctly. However, when the back button is pressed the toolbar slides down but the view does not resize. This means that there's a black strip along the bottom of the view as the other view transitions in. I've tried adding the toolbar's height to the height of the view prior to hiding the toolbar, but this causes the view to be animated during the transition so that there's still a black bar.

I realise I can manage my own UIToolbar, but I'd like to use UINavigationControllers built in UIToolbar for convenience.

This forum post mentions the same issue, but no workaround is mentioned.

like image 911
Nathan de Vries Avatar asked Feb 26 '10 05:02

Nathan de Vries


2 Answers

I too have experienced this problem. In my case, the only way I found to successfully hide the toolbar without showing the background of the window is to call [self.navigationController setToolbarHidden:YES animated:animated] in your view controller’s -viewDidAppear: method.

like image 194
Jeff Kelley Avatar answered Sep 20 '22 14:09

Jeff Kelley


I wasn't satisfied with the answers on this question so I posted my own: Reference to source view controller and destination view controller at the same time

The answer I got fixed my problem. It may work for yours too (though this question is pretty old, I figured this might help someone like me who read this post a half dozen times looking for a hint).

Here's what I did. I don't know if marker protocols are idiomatic objective-c or not, but I liken them to attributes like I'd use in c# so I have this marker protocol:

@protocol HidesNavigationItem @end 

I added UINavigationControllerDelegate to my AppDelegate. I'm not sure yet whether or not that's a good thing. I thought about keeping that implementation in another object, but for now, this is where I put it. Here's the implementation:

#pragma mark Navigation Controller Delegate -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {     [navigationController setNavigationBarHidden:[viewController conformsToProtocol:@protocol(HidesNavigationItem)] animated:animated]; } 

This way, I can just set my marker protocol on my UIViewController implementation like so:

@interface MyViewController : UIViewController <HidesNavigationItem> 

If I don't have that interface, it puts it back.

Finally, in my appDelegate's application:didFinishLaunchingWithOptions: method, I wire up the delegate like this:

if ([self.window.rootViewController isMemberOfClass:[UINavigationController class]])     ((UINavigationController*)self.window.rootViewController).delegate = self; 

Now I get no black boxes and no Cheshire Cat. My solution was with regard to the navigation bar of course, but I'm sure it works the same for the toolbar. This is very similar to Danra's answer except that I get the black box without the "animated:animated."

like image 42
D. Patrick Avatar answered Sep 22 '22 14:09

D. Patrick