ios 如何在 iOS7 中绘制透明的 UIToolbar 或 UINavigationBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18969248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a transparent UIToolbar or UINavigationBar in iOS7
提问by Ben Packard
I would like an entirely transparent UIToolbar
and/or UINavigationBar
. I have tried the various incantations suggested for pre- and post-iOS 5 but none seem to work any more.
我想要一个完全透明的UIToolbar
和/或UINavigationBar
. 我已经尝试了为 iOS 5 之前和之后所建议的各种咒语,但似乎都不再起作用了。
How might this be accomplished in iOS 7?
这如何在 iOS 7 中实现?
回答by Gabriele Petronella
Swift 3 (iOS 10)
斯威夫特 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
目标-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. I'll report here the relevant fragment:
由于文档中讨论的行为,在导航栏上设置translucent
为可以YES
解决问题UINavigationBar
。我将在这里报告相关片段:
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
最后结果
回答by Adam Waite
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];
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
navigationBarAppearance.backgroundColor = [UIColor clearColor];
[navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navigationBarAppearance.shadowImage = [[UIImage alloc] init];
文档:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html
Article: http://nshipster.com/uiappearance/
回答by Leo Natan
Try:
尝试:
[navBar setBackgroundImage:[UIImage alloc] forBarMetrics:UIBarMetricsDefault];
回答by yelled
@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
回答by jakenberg
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. I only tested this in iOS 7.*, but it seemed to work!
我偶然发现,如果我创建了一个子类UINavigationBar
,然后创建了一个空-(void)drawRect:
方法,我会得到一个透明的导航栏。我只在 iOS 7.* 中测试过这个,但它似乎有效!