iOS史上最简单修改导航栏分隔线颜色方法!!!

时间:2021-12-28 06:02:42

  override func viewDidLoad() {
        super.viewDidLoad()
        
       if let imageView = self.findNavLineView(view: navigationBar) {
            // 在分隔线上添加一个跟分隔线大小一模一样的View, 然后修改颜色即可
            let navBarLineView = UIView()
            navBarLineView.frame = imageView.bounds
            navBarLineView.backgroundColor = Color(247, 247, 247, 1)
            imageView.addSubview(navBarLineView)
        }
    }

    // 获取导航栏的分隔线
    func findNavLineView(view: UIView?) -> UIImageView? {
        
        if let view = view {
            if view.isKind(of: UIImageView.self) && view.height <= 1.0 {
                return view as? UIImageView
            }
            
            for subView in view.subviews {
                let imageView = findNavLineView(view: subView)
                if imageView != nil {
                    return imageView
                }
            }
        }
        
        return nil
    }