简体   繁体   English

如何在iOS7中绘制透明的UIToolbar或UINavigationBar

[英]How to draw a transparent UIToolbar or UINavigationBar in iOS7

I would like an entirely transparent UIToolbar and/or UINavigationBar . 我想要一个完全透明的UIToolbar和/或UINavigationBar I have tried the various incantations suggested for pre- and post-iOS 5 but none seem to work any more. 我已经尝试过为iOS 5之前和之后建议的各种咒语,但似乎没有任何工作。

How might this be accomplished in iOS 7? 如何在iOS 7中实现这一目标?

Swift 3 (iOS 10) Swift 3(iOS 10)

Transparent UIToolbar 透明的UIToolbar

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: .any,
                                barMetrics: .default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)

Transparent UINavigationBar 透明的UINavigationBar

self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true

Swift < 3 斯威夫特<3

Transparent UIToolbar 透明的UIToolbar

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)

Transparent UINavigationBar 透明的UINavigationBar

self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true

Objective-C Objective-C的

Transparent UIToolbar 透明的UIToolbar

[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIBarPositionAny
                      barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
          forToolbarPosition:UIBarPositionAny];

Transparent UINavigationBar 透明的UINavigationBar

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

Discussion 讨论

Setting translucent to YES on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar documentation. 由于UINavigationBar文档中讨论的行为,导航栏上的translucent设置为YES解决问题。 I'll report here the relevant fragment: 我将在这里报告相关的片段:

If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image. 如果在具有不透明自定义背景图像的导航栏上将此属性设置为YES ,则导航栏将对图像应用小于1.0的系统不透明度。


Final result 最后结果

最后结果

If you want to do it through the entire app you should use the UIAppearance proxy (iOS5+): 如果您想通过整个应用程序执行此操作,则应使用UIAppearance代理(iOS5 +):

UINavigationBar *navigationBarAppearance = [UINavigationBar appearance]; navigationBarAppearance.backgroundColor = [UIColor clearColor]; [navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; navigationBarAppearance.shadowImage = [[UIImage alloc] init];

Docs: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html 文档: https//developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html

Article: http://nshipster.com/uiappearance/ 文章: http//nshipster.com/uiappearance/

尝试:

[navBar setBackgroundImage:[UIImage alloc] forBarMetrics:UIBarMetricsDefault];
@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

Something I stumbled upon is that if I created a subclassed UINavigationBar and then created an empty -(void)drawRect: method, I would get a transparent navigation bar. 我偶然发现的是,如果我创建了一个子类UINavigationBar然后创建了一个空-(void)drawRect:方法,我会得到一个透明的导航栏。 I only tested this in iOS 7.*, but it seemed to work! 我只在iOS 7. *中测试了这个,但它似乎工作!

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

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