简体   繁体   English

如何在iOS7上使UINavigationBar完全透明?

[英]How can I make my UINavigationBar totally transparent on iOS7?

I'm working on making an application iOS7-compatible, and I'm encountering a problem with UINavigationBar that's driving me crazy: 我正在开发与iOS7兼容的应用程序,并且遇到UINavigationBar的问题,这使我发疯:

I want to make my navigationBar totally transparent, without any blur or backgroundPicture, but containing and displaying navigationItem buttons. 我想使我的navigationBar完全透明,没有任何模糊或backgroundPicture,但包含并显示navigationItem按钮。

In iOS6, I used to make that this way: 在iOS6中,我通常是这样进行的:

UIImage *maskedImage = [UIImage imageNamed:@"transparent_image.png"]
[navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];

But it doesn't work anymore on iOS7. 但它在iOS7上不再起作用。

Any suggestions ? 有什么建议么 ?

Perhaps this will answer your question? 也许会回答您的问题? If you select your view controller and then uncheck the box next to "extend edges under top bars", the background image won't bleed underneath it. 如果您选择视图控制器,然后取消选中“在顶部栏下方扩展边缘”旁边的框,则背景图像将不会在其下方流血。

@implementation MyCustomNavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    [self setupBackground];
}

- (void)setupBackground {
    self.backgroundColor = [UIColor clearColor];
    self.tintColor = [UIColor clearColor];

    // make navigation bar overlap the content
    self.translucent = YES; 
    self.opaque = NO;

    // remove the default background image by replacing it with a clear image
    [self setBackgroundImage:[self.class maskedImage] forBarMetrics:UIBarMetricsDefault];

    // remove defualt bottom shadow
    [self setShadowImage: [UIImage new]]; 
}

+ (UIImage *)maskedImage {
    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [UIImage imageNamed:@"nav-white-pixel-bg.jpg"];
    return [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM