How to remove shadow under UINavigationBar using UISearchController

Image 1

I could successfully remove the shadow under the navigation bar with the next line of code.

self.navigationController?.navigationBar.shadowImage = UIImage()

Image 2

However, when I added the search controller, the shadow reappeared.

self.navigationItem.searchController = UISearchController(searchResultsController: nil)

Image 3

I tried the following, but as a result I got unexpected behavior.

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.isTranslucent = false

Image 4

How to remove the shadow under the navigation bar when the search controller is connected?

+14
source share
4 answers

I also did not find a good solution ...

Now I’ll hide it like this:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let imageView = navigationItem.searchController?.searchBar.superview?.subviews.first?.subviews.first as? UIImageView {
            imageView.isHidden = true
        }
    }
+5
source

Here is the solution I am using

  1. Create a separate class that extends the instance UINavigationController, let's call it BaseNavigationController. Here is your class for you

  2. If you are using storyboards, assign BaseNavigationControlleryour scene a UINavigationController in the storyboard

  3. , : UINavigationController.init(rootViewController: someViewControllerInstance), BaseNavigationController.init(rootViewController: someViewControllerInstance)

:

open class BaseNavigationController:UINavigationController {

    override open func viewDidLoad() {
        super.viewDidLoad()
        setNavigationBar()
        setNavBarBorder(false)
    }

    func setNavigationBar(color:UIColor?=UIColor.white, tint:UIColor?=UIColor.darkGray){
        let appearance = UIBarButtonItem.appearance()
        appearance.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0.0, vertical: 0), for: .default)
        self.navigationBar.barTintColor = color!
        self.navigationBar.isTranslucent = false
        self.navigationBar.tintColor = tint!
        self.navigationBar.titleTextAttributes = [ NSAttributedString.Key.foregroundColor: tint! ]
    }

    func setTitleColor(_ color:UIColor?=UIColor.darkGray){

    }

    func setNavBarBorder(_ enable:Bool) {
        self.navigationBar.setBackgroundImage((enable ? nil : UIImage()), for: UIBarMetrics.default)
        self.navigationBar.shadowImage = (enable ? nil : UIImage())
        self.navigationBar.setValue(true, forKey: "hidesShadow")
    }

}

: , viewWillLayoutSubviews, viewWillAppear. viewController.

(self.navigationController as? BaseNavigationController)?. setNavBarBorder(false)

self.navigationBar.setValue(true, forKey: "hidesShadow"), iOS.

+1

searchBar viewController storyBoard Search Style Minimal, :

enter image description here

0

My little contribution on this. Of course, this is not the right way to do this, but it works for an opaque navigation bar. The idea is to add a view on top of this shadow.

let searchBar = searchController.searchBar
let maskView = UIView(frame: .zero)
maskView.backgroundColor = .white // or any color you want
searchBar.addSubview(maskView)

//We need to use constraint so the mask view follow the search bar animation
maskView.translatesAutoresizingMaskIntoConstraints = false
let views = ["maskView": maskView]
searchBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(0)-[maskView]-(0)-|",
                                                        options: NSLayoutConstraint.FormatOptions.alignAllCenterY,
                                                        metrics: nil,
                                                        views: views))
searchBar.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[maskView(1)]-(-1)-|",
                                                        options: NSLayoutConstraint.FormatOptions.alignAllCenterX,
                                                        metrics: nil,
                                                        views: views))
0
source

Source: https://habr.com/ru/post/1691873/


All Articles