首先,我已经看到了如何在IOS 8中制作UINavigationBar透明的 所有答案?透明UINavigationBar和Make UINavigationBar透明.
他们似乎并不适合我.
我的常规视图控制器(在尝试使导航栏透明之前)没有任何问题:
我正在使用(在viewDidLoad和中试过viewWillAppear:):
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
我明白了:
灰色状态栏背景,完全白色导航栏,不与状态栏混合,然后视图开始.其他问题"答案"中的所有"解决方案"对我来说都会产生相同的结果.
我也尝试过设置self.edgesForExtendedLayout = UIRectEdgeNone;,self.edgesForExtendedLayout = UIRectEdgeAll;但这也没有任何影响.
如何在不弄乱所有内容的情况下使导航栏透明?
更新:在Warif Akhand Rishi的回答之后,我已经改变self.navigationController.view.backgroundColor = [UIColor clearColor];了self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];,现在我得到一个灰色,统一的状态/导航栏,但仍然不透明:
更新2:我已经连接了视图调试器,并且灰色背景似乎来自视图层次结构的根本深处,并且我的视图内容没有扩展.我self.edgesForExtendedLayout = UIRectEdgeAll;再次尝试使用最新的代码,但仍无济于事:
我试图让我的导航栏变为100%透明,这样UINavigationButtonItems只能看到,背景(通常是白色)应该显示背景图像.
我试过了
HomeNavigationController *navBar = [[HomeNavigationController alloc] initWithRootViewController:self.myViewController];
[navBar.navigationBar setBarTintColor:[UIColor clearColor]];
[navBar.navigationBar setTranslucent:YES];
Run Code Online (Sandbox Code Playgroud)
虽然似乎都不起作用.
编辑:
运用
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
我可以看到它按预期工作,但现在这些项目也是看不见的.
FINALEDIT:啊,上面的代码可以工作,只是确保你没有先测试就不对外观进行任何其他改动!
实际执行预期的代码:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
或者,您可以设置透明图像,但这更有意义.如果您不想要代表边框的线,则需要原始编辑中显示的其余代码.