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