Change navigation bar bottom border color Swift

前端 未结 8 1235
时光取名叫无心
时光取名叫无心 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)
    
    0 讨论(0)
  • 2020-12-30 22:40

    From iOS 13 on, you can use the UINavigationBarAppearance() class with the shadowColor property:

    if #available(iOS 13.0, *) {
      let style = UINavigationBarAppearance()
      style.shadowColor = UIColor.clear // Effectively removes the border
      navigationController?.navigationBar.standardAppearance = style
    
      // Optional info for follow-ups:
      // The above will override other navigation bar properties so you may have to assign them here, for example:
      //style.buttonAppearance.normal.titleTextAttributes = [.font: UIFont(name: "YourFontName", size: 17)!]
      //style.backgroundColor = UIColor.orange
      //style.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,
                                   NSAttributedString.Key.font: UIFont(name: "AnotherFontName", size: 20.0)!]
    } else {
      // Fallback on earlier versions
    }
    
    0 讨论(0)
提交回复
热议问题