Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grey background in navigation bar with searchController added to navigationItem during push

I have a table view in navigation controller so that I can push the detail view controller on the stack. It works fine, until I add a search controller to the navigation item, like so:

searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.tintColor = Colors.mlLabel
navigationItem.searchController = searchController
definesPresentationContext = true

It results in temporary grey background, see below:

enter image description here

When debugging the view hierarchy, it looks like UIViewControllerWrapperView's _UIParallaxDimmingView(selected below) is causing this, as both navigation bar and status bar are transparent.

enter image description here

How can I fix this?

Note: Setting the animated property in pushViewController() to false works, but I'd like to keep the animation.

Update: This seems to be issue only on iOS 13. Probably from some recent version even, as I didn't have this issue earlier.

Update 2: I've noticed the same issue on multiple places in my app now, and it's not just in combination with SearchController. Basically the _UIParallaxDimmingView sticks its nose out.

like image 528
Adam Bardon Avatar asked Mar 03 '23 04:03

Adam Bardon


2 Answers

Update

Here's the code I use to go from a large title to a small title. These are properties for the large title viewcontroller, or more specific its navigation controller:

navigationController.navigationBar.prefersLargeTitles = true
navigationController.topViewController?.extendedLayoutIncludesOpaqueBars = true

Perhaps the second line above might help you?

As for pushing any view controllers, I see I've overridden the push-function from the navigation controller (as I use the navigation controller for each tab in my tabbar):

override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    if viewControllers.count >= 1 {
        viewController.hidesBottomBarWhenPushed = true
        viewController.navigationItem.largeTitleDisplayMode = .never
    }
    super.pushViewController(viewController, animated: animated)
}

Previous answer

I saw this a couple of times before in my life and it always had to do something with the background color of the view controller itself. If it's transparent you see this stuff when animating.

But since it's a search controller, it might be the navigation bar. Anyway, since the issue is only since iOS13, I believe the issue can be resolved using this:

searchController.searchBar.backgroundColor = UIColor.clear (or whatever color)

This new property (UISearchBar.searchTextField.backgroundColor) has been added since iOS13, so maybe this will solve it for you? :)

like image 165
Bob de Graaf Avatar answered Apr 09 '23 00:04

Bob de Graaf


I've finally found a solution. One of the problems was that I've set a background color for the navbar like so:

UINavigationBar.appearance().backgroundColor = .white

So removing the above line and adding the below line to the view controller being pushed fixed it.

extendedLayoutIncludesOpaqueBars = true
like image 31
Adam Bardon Avatar answered Apr 08 '23 23:04

Adam Bardon