Updating the navigation bar after a change using UIAppearance

I am currently setting up the background image of the navigation bar of an iOS application using the UIAppearance proxy. There is a button for switching between two different modes that trigger a notification. This notification will change the background to another image using the proxy again. My problem is that this change only becomes visible when I switch to another controller and I return to it. I cannot force update the navigation bar in the controller.

I tried this in my MainTabBarController:

- (void) onAppChangedMode: (NSNotification*)notif { APP_MODE mode = (APP_MODE) [[notif object] integerValue]; // change navigation bar appearance [[UILabel appearance] setHighlightedTextColor:[UIColor redColor]]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:(mode == 0 ? @"navbar.png" : @"navbar2.png")] forBarMetrics:UIBarMetricsDefault]; // trying to update for (UIViewController* vc in self.viewControllers) { [vc.navigationController.navigationBar setNeedsDisplay]; } } 

but nothing ... it does not work. Any idea how to achieve it?

Thanks!

+6
source share
3 answers

Try this code to change the background image for the current navigation bar only :

 [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

Use the above code after changing the UIAppearance. This will change the navigation bar of the current controller. Navigation bars for other controllers will be handled by changing the UIAppearance.

0
source

Just remove the views from the windows and add them again:

 for (UIWindow *window in [UIApplication sharedApplication].windows) { for (UIView *view in window.subviews) { [view removeFromSuperview]; [window addSubview:view]; } } 
+10
source

I just have the same problem, this code will help you:

 - (IBAction)btnTouched:(id)sender { [[UADSwitch appearance]setOnTintColor:[UIColor redColor]]; // Present a temp UIViewController UIViewController *vc = [[UIViewController alloc]init]; [self presentViewController:vc animated:NO completion:nil];//"self" is an instance of UIViewController [vc dismissViewControllerAnimated:NO completion:nil]; } 
+7
source

Source: https://habr.com/ru/post/900189/


All Articles