Tôi tìm thấy một vài vấn đề với các câu trả lời trước:
- nếu bạn thêm một subview: Khi quay của thiết bị, biên giới sẽ không có khung đúng nữa
- nếu bạn đặt ShadowImage để không, sau đó bạn không thể tùy chỉnh bóng của navigationBar.
Một cách giải quyết này sẽ được sử dụng autolayout:
extension UINavigationBar {
func setBottomBorderColor(color: UIColor, height: CGFloat) -> UIView {
let bottomBorderView = UIView(frame: CGRectZero)
bottomBorderView.translatesAutoresizingMaskIntoConstraints = false
bottomBorderView.backgroundColor = color
self.addSubview(bottomBorderView)
let views = ["border": bottomBorderView]
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[border]|", options: [], metrics: nil, views: views))
self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: height))
self.addConstraint(NSLayoutConstraint(item: bottomBorderView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: height))
return bottomBorderView
}
}
Lý do tại sao tôi trở lại biên giới là trong quá trình luân chuyển bạn nhìn thấy nó ở giữa các navigation bar
, vì vậy tôi giấu nó trong vòng quay.
tuyệt vời! Những công trình cho tôi :) Cảm ơn! – Zhen