Swift 修改导航栏底部横线颜色

// 获取导航栏的分隔线
    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
    }

在viewDidLoad中调用

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

你可能感兴趣的:(Swift 修改导航栏底部横线颜色)