如何使UINavigationBar在IOS 8中透明?

我已经尝试了很多,使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] }]; 

尝试这个

  [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]; 

我希望上面的代码有帮助。

尝试添加此代码。 它在iOS 8中为我工作。

 [self.navigationController.navigationBar setTranslucent:YES]; self.navigationController.navigationBar.shadowImage = [UIImage new]; [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; self.navigationController.navigationBar.backgroundColor = [UIColor clearColor] 

使用这个代码,你甚至不需要添加你的透明UIImage 。 更新这里,如果它可以帮助你。

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

 if (IS_OS_7_OR_LATER) { self.edgesForExtendedLayout = UIRectEdgeNone; self.extendedLayoutIncludesOpaqueBars = NO; self.automaticallyAdjustsScrollViewInsets = NO; } 

这就是为什么代码不工作。 当我删除线

 self.edgesForExtendedLayout = UIRectEdgeNone; 

代码工作。

看起来像他拥有它。 在我的应用程序中,我使用

 [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 

唯一的区别是,不是alloc init

尝试这个

  [rootNavC.navigationBar setBackgroundImage:[UIImage imageNamed:@"NAV_BG_iphone.png"] forBarMetrics:UIBarMetricsDefault]; rootNavC.navigationBar.translucent = YES; [[rootNavC.UINavigationBar appearance] setBarTintColor:[UIColor clearColor]]; //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] }]; 

这适用于IOS 7和+

 [self.navigationController.navigationBar setTranslucent:YES]; self.navigationController.navigationBar.shadowImage = [UIImage new]; self.navigationController.view.backgroundColor = [UIColor clearColor]; [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];