iOS11 Can't change shadowImage on UINavigationBar when searchController is set

Hey,


Am building the new version of an app for iOS 11. Am using the large title setup with native controls to achieve an interaction pretty much the same as the contacts app's main view.


My problem is that when I set the searchController so that the UISearchBar appears in the navigation bar, any appearance or manual setting of the shadowImage on the UINavigationBar gets ignored.


Here are the setters I am using (UIImage withColor is custom helper that returns a resizeable image of given color).


...
navigationController.navigationBar.prefersLargeTitles = true
navigationController.navigationItem.largeTitleDisplayMode = .automatic
...
navigationController.navigationBar.shadowImage = UIImage(withColor: UIColor.white)
...


When I set the searchController here (either before or after setting the navigation settings):


searchController = UISearchController(searchResultsController: resultsTableController)
...
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false


The shadow separator image is ignored and a black 0.5pt (or whatever size) shadowImage is shown regardless of if I set it again.


I can't seem to fix this - it's not a dealbreaker - but the design I have want's it not there and I would rather not have to do any hacky subview searching/class from string searching etc to fix it.


Has anyone else had this issue, or know if it is even possible to customize the `shadowImage` property if a `searchController` is set on the `navigationItem`


Cheers,


Michael

Replies

I'm in the same boat. I was able to remove the black line by changing the barStyle to ".black", but it replaced it with what looks like an image with an alpha of 0.5, so I get a lighter version of the color of both my nav bar and search bar. Setting the shadowImage to a transparent image does nothing.

Can you report this issue as a bug? I have same problem with shadow image under navigation bar with search controller or search bar.

Same thing here...

Seems to be a bug in iOS 11.


Checking the view hierarchy Apple is now using two shadow image views - one when there is searchController and one when there is none. The shadow image view also is not a subview of the navigation bar.


Here is a hack I have used to fix this:

func fixShadowImage(inView view: UIView) {
    if let imageView = view as? UIImageView {
        let size = imageView.bounds.size.height
        if size <= 1 && size > 0 &&
            imageView.subviews.count == 0,
            let components = imageView.backgroundColor?.cgColor.components, components == [1, 1, 1, 0.15]
        {
            print("Fixing shadow image")
            let forcedBackground = UIView(frame: imageView.bounds)
            forcedBackground.backgroundColor = .white
            imageView.addSubview(forcedBackground)
            forcedBackground.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        }
    }
    for subview in view.subviews {
        fixShadowImage(inView: subview)
    }
}


Search for image view with height between in 0 and 1 with white color and 0.15 alpha.

If found, add a subview with custom background color.


Usage:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if let window = view.window {
        fixShadowImage(inView: window)
    }
}

I still see this issue with iOS 11.4 on devices. I see it on iOS 12 beta 1 simulators, too. Wondering when Apple is going to fix this issue.

any progress?