iOS7 - Change UINavigationBar border color

后端 未结 15 2056
时光取名叫无心
时光取名叫无心 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:17

    Based on the answer from @sash I made an extension in Swift using Autolayout, explained right here.

    In essence, the other solutions have the following pitfalls:

    • Cannot add dropshadow if using the UIImage solution
    • The subview added doesn't resize upon rotation of the view
    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
      }
    }
    

    This let you still add a drop shadow if you need to, and this handles rotation nicely !

提交回复
热议问题