使用UIAppearance更改后更新导航栏

Cla*_*aus 7 background updates uinavigationbar ios uiappearance

我目前正在使用UIAppearance代理自定义iOS应用程序的导航栏背景图像.有一个用于在两种不同模式之间切换的按钮,用于触发通知.此通知将再次使用代理将背景更改为其他图像.我的问题是,只有当我去另一个控制器并且我回到它时,这个变化才变得可见.我无法强制更新控制器中的导航栏.

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

}
Run Code Online (Sandbox Code Playgroud)

但没什么......它不起作用.知道如何实现它吗?

谢谢!

avd*_*hin 11

只需从Windows中删除视图并再次添加它们:

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


Jav*_*van 7

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)


Yan*_*Tay 0

尝试使用此代码仅更改当前导航栏的背景图像:

[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

更改 UIAppearance 后使用上面的代码。这将强制更改当前控制器的导航栏。其他控制器的导航栏将通过 UIAppearance 的更改来处理。

  • 编辑了我的答案。将上面的代码与更改 UIAppearance 的代码一起使用。 (2认同)