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

语言: CN / TW / HK

“我正在参加「掘金·启航计划」”

引言

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

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

I 导航栏的黑边设置

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

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

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

```

iOS15之后 ```objectivec

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适配)

```objectivec

// 设置导航栏下边的黑边 + (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的顶部黑线

  • setupshadowColor ```objectivec

  • (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适配) http://blog.csdn.net/z929118967/article/details/104276156