无法在iOS6中使导航栏完全透明

时间:2012-11-12 05:10:56

标签: ios uikit ios6 uinavigationbar

我使用以下代码在iOS5中使导航栏透明:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];

升级到iOS6并且导航栏仍然是透明的,但现在它下面有一条细黑线。如何使导航栏完全透明?

我也尝试了以下所有方法:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

提前致谢。

3 个答案:

答案 0 :(得分:6)

解决。 iOS6为导航栏添加了阴影。所以我在iOS5上使用的屏蔽代码仍然可以正常工作 - 我只需要添加

if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)]) 
{ 
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]]; 
} 

摆脱投影。

答案 1 :(得分:2)

    self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [[UIImage alloc] init];
    UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

    [self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
    [[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];

答案 2 :(得分:1)

 if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])           
 { 
    [self.navigationController.navigationBar setShadowImage:[[[UIImage alloc] init] autorelease]]; 
    // autorelease is necessary, or else [[UIImage alloc] init]'s retainCount is 2.
 }