7

我目前正在使用 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];
}

}

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

谢谢!

4

3 回答 3

12

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

for (UIWindow *window in [UIApplication sharedApplication].windows) {
    for (UIView *view in window.subviews) {
        [view removeFromSuperview];
        [window addSubview:view];
    }
}
于 2015-02-02T09:26:44.593 回答
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];
}
于 2013-05-27T07:25:34.717 回答
0

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

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

更改 UIAppearance 后使用上述代码。这将强制更改当前控制器的导航栏。其他控制器的导航栏将由 UIAppearance 中的更改处理。

于 2013-02-23T00:04:34.107 回答