iOS13.0前后导航栏的透明、背景色、下划线、返回按钮的处理
最近接手项目,里面关于导航栏的定义非常乱,然后自己抽空写了个demo,里面有个分类,专门总结梳理了一下关于iOS13.0前后的对于导航条的处理方式:
分类的方法
1.关于下划线的处理:
-(void)setNoLine
{
if (@available(iOS 13.0, *)) {
self.standardAppearance.shadowColor = [UIColor clearColor];
} else {
if (@available(iOS 10.0, *))
{
[self getLineHidden:YES];
}else
{
self.shadowImage = [UIImage new];
}
}
}
2.关于背景色的处理:
- (void)setBackColor:(UIColor *)backColor
{
if (@available(iOS 13.0, *))
{
[self.standardAppearance configureWithOpaqueBackground];
[self.standardAppearance setBackgroundColor:backColor];
[self.standardAppearance setBackgroundImage:nil];
}else
{
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setBarTintColor:backColor];
}
}
3.关于导航条透明的处理:
- (void)setBackClear
{
if (@available(iOS 13.0, *))
{
[self.standardAppearance configureWithTransparentBackground];
self.standardAppearance.shadowColor = [UIColor clearColor];
}else
{
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
if (@available(iOS 10.0, *))
{
[self getLineHidden:YES];
}else
{
self.shadowImage = [UIImage new];
}
}
}
4.关于重置导航条为系统的样式处理:
- (void)resetBack
{
if (@available(iOS 13.0, *))
{
[self.standardAppearance configureWithDefaultBackground];
self.standardAppearance.shadowImage = nil;
}else
{
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
if (@available(iOS 10.0, *))
{
[self getLineHidden:NO];
}else
{
self.shadowImage = nil;
}
}
}
5.ios10上关于是否隐藏下划线的处理:
- (void)getLineHidden:(BOOL)hidden
{
if ([self respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)])
{
NSArray *list=self.subviews;
for (id obj in list)
{
UIView *view = (UIView*)obj;
for (id obj2 in view.subviews) {
if ([obj2 isKindOfClass:[UIImageView class]])
{
UIImageView *image = (UIImageView*)obj2;
image.hidden = hidden;
}
}
}
}
}
6.关于设置导航条标题font和color的处理:
- (void)setTitleColor:(UIColor *)titleColor font:(UIFont *)font
{
//文字颜色
if (@available(iOS 13.0, *))
{
[self.standardAppearance setTitleTextAttributes:@{NSFontAttributeName:font , NSForegroundColorAttributeName:titleColor}];
}else
{
[self setTitleTextAttributes:@{NSFontAttributeName:font , NSForegroundColorAttributeName:titleColor}];
}
}
7.传入一个返回按钮图片名字,自定义导航条返回按钮样式的处理:
- (void)setBackImageWithImageName:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
if (@available(iOS 13.0, *))
{
[self.standardAppearance setBackIndicatorImage:image transitionMaskImage:image];
}else
{
self.backIndicatorImage = image;
self.backIndicatorTransitionMaskImage = image;
}
}
具体demo地址:https://github.com/shineDongDongEr/LDLNavBar-Sxs,给我一颗小💖,😃!!