如何在NavigationBar中全局添加阴影并删除底线?

时间:2019-05-30 05:21:22

标签: ios swift uinavigationcontroller uinavigationbar uinavigationitem

所以我正在寻找如何向NavigationBar添加阴影并从NavigationBar删除底部的黑色线条?另外,如果我在NavigationBar中具有searchBar,则阴影应位于搜索栏的下方。我想在全局范围内进行更改,而不在每个viewController中进行更改。

https://imgur.com/a/8ogGRaf.jpg

所以我已经存档了这些更改,只是在搜索控制器下方添加了一个阴影。

  1. 对于阴影:

    self.navigationController?.navigationBar.isTranslucent = false
    self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
    self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    self.navigationController?.navigationBar.layer.shadowRadius = 7.0
    self.navigationController?.navigationBar.layer.shadowOpacity = 0.2
    
  2. 用于删除底部栏:

    UINavigationBar.appearance().backIndicatorImage = UIImage(named:"normal")
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named:"normal")
    

这是我在搜索栏中遇到的问题

https://imgur.com/a/QQ9yrbE

1 个答案:

答案 0 :(得分:0)

2。隐藏底线

public extension UINavigationBar {

    /// Hide line under navigation bar
    public func hideBottomHairline() {
        let navigationBarImageView = hairlineImageViewInNavigationBar(self)
        navigationBarImageView!.isHidden = true
    }

    ///  Show line under navigation bar
    public func showBottomHairline() {
        let navigationBarImageView = hairlineImageViewInNavigationBar(self)
        navigationBarImageView!.isHidden = false
    }

    fileprivate func hairlineImageViewInNavigationBar(_ view: UIView) -> UIImageView? {
        if view.isKind(of: UIImageView.self) && view.bounds.height <= 1.0 {
            return (view as! UIImageView)
        }
        let subviews = (view.subviews as [UIView])
        for subview: UIView in subviews {
            if let imageView: UIImageView = hairlineImageViewInNavigationBar(subview) {
                return imageView
            }
        }
        return nil
    }   
}