iOS 导航栏UINavigationBar隐藏底部线及透明度
苹果官方给出的解释:
如果你不调用方法设置一张背景图片的话,那就给你默认一张,然后同时还有一张阴影图片被默认设置上去,这就是导航栏上1px黑线的由来。
解决办法:
方法1.就用上面说的那个方法,设置一张背景图片,然后在设置一张shadowImage就可以了:
self.navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]];
[self.navigationBar setShadowImage:[[UIImage alloc] init]]];
方法2:将UINavigationBar的clipsToBounds属性设成YES。
self.navigationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
self.navigationBar.clipsToBounds=YES;
//去掉导航条的半透明
self.navigationBar.translucent = NO;
方法3:循环遍历一下UINavigationBar的所有子视图,发现有UIImageView类型的视图就remove掉,或者设成隐藏状态(hidden)。
- (void)setNavigationStyle {
[self setBackgroundImage];
UIColor *color = K_NAVBAR_ITEM;
if (IS_IOS7_OR_LATER) {
self.navigationBar.tintColor = color;
self.navigationBar.barTintColor = color;
} else {
self.navigationBar.tintColor = color;
}
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"ffffff"],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
}
- (void)setBackgroundImage {
UIImage *placeholderImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"3b93ff"] size:CGSizeMake(20.0, 20.0)];
placeholderImage = [placeholderImage stretchableImageWithLeftCapWidth:placeholderImage.leftCapWidth*0.5 topCapHeight:placeholderImage.topCapHeight*0.5];
if (IS_IOS7_OR_LATER) {
[self.navigationBar setBackgroundImage:placeholderImage forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
//iOS7 阴影需单独设定 UIColor clearColor 是去掉字段 1像素阴影]
[self.navigationBar setShadowImage:[UIImage imageWithColor:RGBA(37.0, 37.0, 37.0, 0.8) size:CGSizeMake(self.navigationBar.frame.size.width, 0.125)]];
} else {
[self.navigationBar setBackgroundImage:placeholderImage forBarMetrics:UIBarMetricsDefault];
}
self.navigationBar.translucent = NO;
self.navigationBar.clipsToBounds=YES;
}
学习记录,每天不停进步。