如何在iOS 7上获得不透明的UINavigationBar

29

有没有办法让针对 iOS 7 的 UINavigationBar 变成纯色而不透明?我已经尝试过设置backgroundColorbarTintColoropaquetransparent/translucent等属性,但都无效。

有人知道怎么做吗?这里是一些我一直在使用的代码片段。请注意我设置的 navigationBar 属性。它们以任何顺序或组合都没用过。

@property (strong, nonatomic) UITabBarController *tabBarController;

testViewController = [[TestViewController alloc] init];
anotherViewController = [[AnotherViewController alloc] init];
navigationController = [[UINavigationController alloc]
                            initWithRootViewController:testViewController];

navigationController.navigationBar.barTintColor = [UIColor darkGrayColor;
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = YES;
navigationController.navigationBar.opaque = YES;

NSArray *tabBarViewControllers = [NSArray arrayWithObjects:
                                      navigationController,
                                      anotherViewController, nil];
self.tabBarController.viewControllers = tabBarViewControllers;

请发布您设置半透明的代码。 - Tarek Hallak
4
请在你的 testViewController 中尝试加入 self.navigationController.navigationBar.translucent = NO;。这行代码的作用是取消导航栏的半透明效果。 - Tarek Hallak
4个回答

56

你的代码列举了navigationController.navigationBar.translucent = YES;

将其设置为NO。


9
要使其完全不透明,您可以直接从属性部分更改导航栏的样式!只需将样式设置为“黑色”即可。这是当您将其变为黑色时的外观。

我会尝试将其翻译成代码。我不使用XIB文件。谢谢。 - tentmaking
似乎没有起作用。当我将barStyle设置为UIBarStyleBlack时,我的UITableView项目仍然显示在UINavigationBar中。 - tentmaking
然后尝试移除透明效果。 - votelessbubble
好的,根据您提供的细节,我了解到您在一个视图控制器中有一个UINavBar和UITableView,对吗? 我之前也遇到过这种问题,所以我使用了一个容器视图来处理我的UITableView。 我将容器视图放在导航栏所在的视图控制器上,将表视图放在容器的视图控制器中! 希望这个方法能够解决您的问题! - votelessbubble

7
为了使您的所有导航栏不透明,您只需在application:didFinishLaunchingWithOptions:中添加UINavigationBar.appearance().translucent = false即可。

1
self.navigationController.navigationBar.isTranslucent = false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接