I'm using a UINavigationController
to display some view controllers. I need to change the color of the navigation bar title every time I switch between two view controllers. This is what I'm doing now:
First View Controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.titleTextAttributes =
@{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont systemFontOfSize:14.0]
};
}
Second View Controller
- (void)viewDidLoad:(BOOL)animated
{
[super viewDidLoad:animated];
self.navigationController.navigationBar.titleTextAttributes =
@{
NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:14.0]
};
}
The first time I load First VC and when I push Second VC, the title color is handled correctly. The problem here is that when I pop from Second to First view controller, the title is still black, even if viewWillAppear
is called correctly and, if I print self.navigationController.navigationBar.titleTextAttributes
, the values seems to be updated (NSForegroundColorAttributeName
is white).
Maybe because of the push/pop transition animation, values are not reflecting. Try calling it this way.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationController.navigationBar.titleTextAttributes =
@{
NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:14.0]
};
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With