使用UIAppearance更新后更新导航栏

我目前正在使用UIAppearance代理自定义iOS应用的导航栏背景图片。 有一个button可以在两种不同的模式之间切换,从而触发通知。 此通知将使用代理再次将背景更改为不同的图像。 我的问题是,只有当我去一个不同的控制器,我才能回到这个变化。 我无法强制更新控制器内的导航栏。

我已经在我的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]; } } 

但没有什么…它不工作。 任何想法如何实现它?

谢谢!

试试这段代码只能改变当前导航栏的背景图片:

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

改变UIA外观后使用上面的代码。 这将强制当前控制器的导航栏中的更改。 其他控制器的导航栏将由UIA的外观变化来处理。

从窗口中删除视图并再次添加它们:

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

我只是有同样的问题,这段代码将帮助你:

 - (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]; }