Change navigation bar bottom border color Swift

前端 未结 8 1299
时光取名叫无心
时光取名叫无心 2020-12-30 21:36

It works with

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup a         


        
8条回答
  •  感动是毒
    2020-12-30 22:36

    Solution for Swift 4.0 - 5.2

    Here is small extension for changing both Height and Color of bottom navbar line

    extension UINavigationController
    {
        func addCustomBottomLine(color:UIColor,height:Double)
        {
            //Hiding Default Line and Shadow
            navigationBar.setValue(true, forKey: "hidesShadow")
        
            //Creating New line
            let lineView = UIView(frame: CGRect(x: 0, y: 0, width:0, height: height))
            lineView.backgroundColor = color
            navigationBar.addSubview(lineView)
        
            lineView.translatesAutoresizingMaskIntoConstraints = false
            lineView.widthAnchor.constraint(equalTo: navigationBar.widthAnchor).isActive = true
            lineView.heightAnchor.constraint(equalToConstant: CGFloat(height)).isActive = true
            lineView.centerXAnchor.constraint(equalTo: navigationBar.centerXAnchor).isActive = true
            lineView.topAnchor.constraint(equalTo: navigationBar.bottomAnchor).isActive = true
        }
    }
    

    And after adding this extension, you can call this method on any UINavigationController (e.g. from ViewController viewDidLoad())

    self.navigationController?.addCustomBottomLine(color: UIColor.black, height: 20)
    

提交回复
热议问题