注册
iOS

iOS小技能:去掉/新增导航栏黑边(iOS13适配)

引言


背景: 去掉导航栏下边的黑边在iOS15失效
原因:必须使用iOS13之后的APIUINavigationBarAppearance设置才能生效

UIKIT_EXTERN API_AVAILABLE(ios(13.0), tvos(13.0)) NS_SWIFT_UI_ACTOR
@interface UINavigationBarAppearance : UIBarAppearance

I 导航栏的黑边设置


1.1 去掉导航栏下边的黑边(iOS15适配)


4693711d4cc89750b41722b85c942e56.png


iOS15之前: [self.navigationBar setShadowImage:[[UIImage alloc] init]];

        [vc.navigationController.navigationBar setBackgroundImage:[ImageTools createImageWithColor: [UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];


iOS15之后


if(@available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];

//去掉透明后导航栏下边的黑边
appearance.shadowImage =[[UIImage alloc] init];

appearance.shadowColor= UIColor.clearColor;



navigationBar.standardAppearance = appearance;

navigationBar.scrollEdgeAppearance = appearance;

}

1.2 设置导航栏下边的黑边(iOS13适配)


5191fcb563d0d72c6ae9bc21fe6c438b.png



// 设置导航栏下边的黑边
+ (void)setupnavigationBar:(UIViewController*)vc{



if (@available(iOS 13.0, *)) {

UINavigationBar *navigationBar = vc.navigationController.navigationBar;

UINavigationBarAppearance *appearance =navigationBar.standardAppearance;


appearance.shadowImage =[UIImage createImageWithColor:k_tableView_Line];

appearance.shadowColor=k_tableView_Line;


navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = appearance;

} else {
// Fallback on earlier versions

UINavigationBar *navigationBar = vc.navigationController.navigationBar;
[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; //此处使底部线条颜色为红色
// [navigationBar setShadowImage:[UIImage createImageWithColor:[UIColor redColor]]];

[navigationBar setShadowImage:[UIImage createImageWithColor:k_tableView_Line]];

}



}




II 去掉TabBar的顶部黑线


00e77b73fc02d8fbd312c2fc2549217b.png

  • setupshadowColor


- (void)setupshadowColor{

UIView * tmpView = self;
tmpView.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影的颜色
tmpView.layer.shadowOpacity = 0.08;//设置阴影的透明度
tmpView.layer.shadowOffset = CGSizeMake(kAdjustRatio(0), kAdjustRatio(-5));//设置阴影的偏移量,阴影的大小,x往右和y往下是正
tmpView.layer.shadowRadius = kAdjustRatio(5);//设置阴影的圆角,//阴影的扩散范围,相当于blur radius,也是shadow的渐变距离,从外围开始,往里渐变shadowRadius距离


//去掉TabBar的顶部黑线
[self setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor]]];
[self setShadowImage:[UIImage createImageWithColor:[UIColor clearColor]]];

}


see also


iOS小技能:自定义导航栏,设置全局导航条外观。(iOS15适配)
blog.csdn.net/z929118967/…


作者:公众号iOS逆向
链接:https://juejin.cn/post/7146388120076812324
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 个评论

要回复文章请先登录注册