Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating navigation bar after a change using UIAppearance

I'm currently customising the navigation bar background image of my iOS app using the UIAppearance proxy. There is a button for switching between two different modes which triggers a notification. This notification will change the background to a different image using again the proxy. My problem is that this change becomes visible only when I go to a different controller and I come back to it. I'm not able to force the update of the navigation bar within the controller.

I've 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's not working. Any idea how to achieve it?

Thanks!

like image 581
Claus Avatar asked Feb 22 '13 12:02

Claus


People also ask

What is Uinavigationcontroller for?

A navigation controller is a container view that can manage the navigation of hierarchical contents. The navigation controller manages the current displaying screen using the navigation stack. Navigation stack can have “n” numbers of view controllers.

How do I change the navigation bar color in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.

What is navigation in iOS?

June 03, 2019. A navigation controller is responsible for managing the navigation of hierarchical content. The navigation controller manages the current displayed screens using the navigation stack. At the bottom of this stack is the root view controller and at the top is the view controller currently displayed.


1 Answers

Just remove views from windows and add they again:

for (UIWindow *window in [UIApplication sharedApplication].windows) {
    for (UIView *view in window.subviews) {
        [view removeFromSuperview];
        [window addSubview:view];
    }
}
like image 91
avdyushin Avatar answered Jan 26 '23 01:01

avdyushin