iOS7 - Change UINavigationBar border color

后端 未结 15 2048
时光取名叫无心
时光取名叫无心 2020-12-07 14:44

Is it possible to change the grey border-bottom color of the UINavigationBar in iOS7?

I already tried to remove to border, but this is not working:

[         


        
15条回答
  •  眼角桃花
    2020-12-07 15:34

    To build on @sash's Swift implementation you can make the border responsive to rotation/trait changes by using constraints:

    extension UINavigationBar {
      func setBottomBorderColor(color: UIColor, height: CGFloat) {
    
        let bottomBorderView = UIView()
        bottomBorderView.backgroundColor = color
        bottomBorderView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(bottomBorderView)
    
        // Add constraints to make the bar always stay at the bottom of the nav bar and change size with rotation/trait changes
        let horizontalConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
        let verticalConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
        let widthConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .width, multiplier: 1, constant: 0)
        let heightConstraint = NSLayoutConstraint(item: bottomBorderView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: height)
    
        self.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
      }
    }
    

提交回复
热议问题