Tuesday, April 19, 2016

Buttons on navigation bar do not pick up window tint colour

Leave a Comment

I have an Objective-C app that runs on the iPad and displays a view controller with a modal presentation style of UIModalPresentationPageSheet:

UINavigationController *editorNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController]; editorNavigationController.modalPresentationStyle = UIModalPresentationPageSheet; [navigationController presentViewController:editorNavigationController animated:YES completion:nil]; 

When this view controller is displayed the buttons in the navigation bar are purple, which I assume has been picked up from the window's tint colour, which is what I want.

Later I need to display another view controller over the top, that fills the whole window:

UINavigationController *previewNavigationController = [[UINavigationController alloc]initWithRootViewController:myPreviewViewController]; [owningViewController presentViewController:previewNavigationController animated:YES completion:nil]; 

The problem I have is that when myPreviewController is displayed, the buttons in the navigation bar are grey. I've tried reinstating the colour on the new navigation controller:

previewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:123/255.0 green:26/255.0 blue:69/255.0 alpha:1]; 

but without any joy.

How can I get the buttons to have the correct colour? Can I get the new navigation controller to pick up the window tint colour automatically, or do I have to set it explicitly? Is this something to do with presenting the second navigation controller over the top of one that uses UIModalPresentationPageSheet?

Any help much appreciated! Thanks,

Paul

2 Answers

Answers 1

You can set the navigationBar translucent and transparent. In view Will Appear:

self.navigationController.navigationBar.translucent = NO; 

Than create a UIView with frame size of navigationBar (CGRectMake(0,0,self.view.frame.size.height,22) and create buttons on it with color you need. I know, thats a crutch but should work)

Answers 2

You can change the appearance of the UIBarButtonItem globally so all UINavigationControllers will share the same design:

[[UIBarButtonItem appearance] setTintColor:[UIColor purpleColor]]; [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:                                                           [UIFont fontWithName:@"fontName" size:16.0f],NSFontAttributeName,                                                           nil] forState:UIControlStateNormal];  

Additionally you could also change the [UINavigationBar appearance]:

//The setTintColor would tint the < Back button in the NavigationBar  [[UINavigationBar appearance] setTintColor:[UIColor greenColor]]; [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; [[UINavigationBar appearance] setTitleTextAttributes:      @{NSForegroundColorAttributeName:[UIColor blueColor]}]; 

This code can be add it before presenting the UIViewControllers or simply in the AppDelegate.m.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment