Change navigation bar bottom border color Swift

前端 未结 8 1233
时光取名叫无心
时光取名叫无心 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:30

    Putting @alessandro-orru's answer in one extension

    extension UINavigationController {
    
        func setNavigationBarBorderColor(_ color:UIColor) {
            self.navigationBar.shadowImage = color.as1ptImage()
        }
    }
    
    extension UIColor {
    
        /// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it.
        ///
        /// - Returns: `self` as a 1x1 `UIImage`.
        func as1ptImage() -> UIImage {
            UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
            setFill()
            UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    then in your view controller just add:

    self.navigationController?.setNavigationBarBorderColor(UIColor.red)
    

提交回复
热议问题