我想要一个完全透明UIToolbar
和/或UINavigationBar
。我尝试了针对iOS 5前后的各种建议,但似乎都无法正常工作了。
在iOS 7中如何实现?
Answers:
UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: .any,
barMetrics: .default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.Any,
barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
forToolbarPosition: UIBarPosition.Any)
UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
UIToolbar
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIBarPositionAny];
UINavigationBar
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
由于文档中讨论的行为,在导航栏上设置translucent
为可以YES
解决问题UINavigationBar
。我将在这里报告相关片段:
如果将此属性设置为
YES
带有不透明自定义背景图像的导航栏,则导航栏将对图像应用小于1.0的系统不透明度。
iOS 7
模拟器
如果要通过整个应用程序进行操作,则应使用UIAppearance代理(iOS5 +):
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
UINavigationController
子类一起使用,即要将此行为应用于的子类。
@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