ios - 不同 View 的不同 UINavigationBar 背景

标签 ios objective-c uinavigationbar appearance

我正在用这样的代码创建我的 UINavigationBar:

// init and add root navigation controller to view
_rootNavigationController = [[UINavigationController alloc] init];
[_rootView.contentContainer addSubview:_rootNavigationController.view];
_rootNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

然后在某个 UIViewController 中,我希望 UINavigationBar 有一个清晰的背景,所以我使用这段有效的代码:

UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.topItem.title = @"";
[navBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:[UIImage new]];
[navBar setTranslucent:YES];
[navBar setTintColor:[[AVOThemeManager sharedTheme] contentBackButtonColor]];

一旦用户通过按返回离开此页面,我想将我的 UINavigationBar 返回到它原来的样子,所以我正在尝试这样做:

- (void)viewDidDisappear:(BOOL)animated {
    UINavigationBar *navBar = self.navigationController.navigationBar;
    [navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [navBar setShadowImage:nil];
    [navBar setBarStyle:UIBarStyleBlackTranslucent];
    [navBar setTranslucent:YES];
}

但这似乎行不通。我也不想将 UINavigationBar 设置为一个新实例,然后设置属性,因为它有一个添加到根导航 Controller 中的全局按钮, subview Controller 不知道。

还有什么我可以尝试的吗?

最佳答案

发帖后,马上就找到了答案。基本上,我不应该使用 viewDidDisappear,而应该使用 viewWillDisappear,它起作用了。

关于ios - 不同 View 的不同 UINavigationBar 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431634/

相关文章:

iphone - 应用程序未能及时启动

iphone - 在字符串文件中存储键/值对

javascript - 为什么iOS上three.js的glTFLoader不显示Monster模型?

ios - SwiftUI-HStack NavigationLink“按钮”分别向两个元素添加按钮样式?

ios - 如何在swift中动态创建单选按钮?

iphone - iOS 中的通话和短信计数器

iOS - 提高 SQLite 的插入性能

iphone - objective-c 中导航栏标题中的图像

iphone - UINavigationController 导航栏在横向模式下不会缩小

ios - 如何在导航栏右侧添加多个 UIBarButtonItems?