消除 UINavigationBar 底部黑线

1. 通过设置背景图片与阴影图片清除

1
2
3
// 在自定义 UINavigationController 内
self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationBar.shadowImage = UIImage()

2. 通过遍历 UINavigationBar 子视图,找到对应的 View,设置为隐藏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
extension UINavigationBar {

func hideBottomHairline() {
findUnderImageView(self)?.isHidden = true
}

func showBottomHairline() {
findUnderImageView(self)?.isHidden = true
}


func findUnderImageView(_ view: UIView) -> UIImageView? {

if view is UIImageView && view.bounds.height <= 1.0 {
return view as? UIImageView
}

for subView in view.subviews {
return self.findUnderImageView(subView)
}
return nil
}
}

使用时只需在 viewWillAppear 方法中添加 self.navigationController?.navigationBar.hideBottomHairline()