如何在IOS 8中使UINavigationBar透明?

Mou*_*ala 6 iphone ios

我已经尝试了很多让UINavigationBar透明的东西.但我没有成功.我设置的图像是透明的.这是我的代码.有帮助吗?提前致谢.

 [rootNavC.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAV_BG_iphone.png"] forBarMetrics:UIBarMetricsDefault];
rootNavC.navigationBar.translucent = YES;
rootNavC.navigationBar.backgroundColor = [UIColor clearColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{
                           UITextAttributeTextColor : [UIColor whiteColor],
                     UITextAttributeTextShadowColor : [UIColor clearColor],
                    UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                UITextAttributeFont : [UIFont fontWithName:@"pastel" size:20]
 }];
Run Code Online (Sandbox Code Playgroud)

小智 15

试试这个

   [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)

我希望上面的代码有所帮助.


Sus*_*rma 9

尝试添加此代码.它在iOS 8中适用于我.

[self.navigationController.navigationBar setTranslucent:YES];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]
Run Code Online (Sandbox Code Playgroud)

使用此代码,您甚至不需要添加透明UIImage.如果有帮助,请在这里更新.


Mou*_*ala 6

谢谢大家.问题是我在视图控制器中添加了这一行:

if (IS_OS_7_OR_LATER)
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
Run Code Online (Sandbox Code Playgroud)

这就是代码无效的原因.当我删除该行

self.edgesForExtendedLayout = UIRectEdgeNone;
Run Code Online (Sandbox Code Playgroud)

代码有效.