我已经将导航栏只舍入了底部的两个角,但是当我使用clipsToBounds = true时,它也修剪了导航栏的顶部。在下图中,我希望整个条形为橙色,但只有一部分为橙色。
import UIKit
class MyNavBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
setupNavBar()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupNavBar()
}
func setupNavBar() {
tintColor = .white
barTintColor = .orange
layer.cornerRadius = 20
layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
clipsToBounds = true
}
}
您不应修改UINavigationBar
视图层次结构或更改图层属性。有一种专用的方法setBackgroundImage(_:for:barMetrics:)
可自定义UINavigationBar
外观。我建议在Apple's UIKit Documentation here 👈中选中自定义导航栏的外观。
为了允许对导航栏的外观进行完全自定义,您还可以提供自定义背景和阴影图像。要提供自定义背景图像,请使用
setBackgroundImage(_:for:barMetrics:)
方法,为适当的条形位置和度量值提供一个UIImage对象。