I've got a question about setting different styles of the navigationbar in different UIViewControllers. I've got 4 different controllers and I want the last one to be totally transparent with white navigationitems and the other one to be white with black navigationitems.
Is there an quick and easy solution for this? I am thinking about setting the style for each view in the appdelegate
What you need to do is save the navigation bar tintColor
and barTintColor
before the view appears and change it to whatever you need. Then when the view disappears, restore the previous ones.
@interface MyViewController ()
@property (strong, nonatomic) UIColor *navigationBarTintColor;
@property (strong, nonatomic) UIColor *navigationTintColor;
@end
@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Save current colors
self.navigationBarTintColor = self.navigationController.navigationBar.barTintColor; // Background color
self.navigationTintColor = self.navigationController.navigationBar.tintColor; // Items color
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Get previous colors and set them
self.navigationController.navigationBar.barTintColor = self.navigationBarTintColor;
self.navigationController.navigationBar.tintColor = self.navigationTintColor;
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
EDIT: Only use this code in the view controller that needs to have a transparent navigation bar.