2
votes

I'm using an UISearchController on a navigation item (the navigation bar has been setup to use barTintColor and tintColor). When the search controller is dismissed (tapping the Cancel button of the search bar), the back button's tintColor is reversed back to default (blue).

Screenshot 1: The tintColor for the navigation bar is .white enter image description here

Screenshot 2: When the UISearchController is active: enter image description here

Screenshot 3: When the UISearchController is dismissed (by tapping Cancel) - you could see the tintColor for the Back button is reversed back to default (blue), not white: enter image description here

The code on the screenshots is:


/// View controller is embedded in a UINavigationController, the root view controller of the startup storyboard.
class ViewController: UIViewController {

  lazy var searchController: UISearchController = {
    let controller = UISearchController(searchResultsController: nil)
    controller.searchBar.tintColor = .white
    return controller
  }()

  override func viewDidLoad() {
    super.viewDidLoad()
    if let navigationBar = navigationController?.navigationBar {
      setupNavigationBar(navigationBar)
    }
    if #available(iOS 11.0, *) {
      navigationItem.searchController = searchController
    } else {
      navigationItem.titleView = searchController.searchBar
    }
  }

  func setupNavigationBar(
    _ navigationBar: UINavigationBar,
    barTintColor: UIColor = .red,
    tintColor: UIColor = .white,
    textColor: UIColor = .white,
    prefersLargeTitles: Bool = true,
    isTranslucent: Bool = false) {

    navigationBar.isTranslucent = isTranslucent
    navigationBar.barTintColor = barTintColor

    if #available(iOS 11.0, *) {
    } else {
      navigationBar.setBackgroundImage(UIImage(), for: .default)
    }
    navigationBar.shadowImage = UIImage()

    navigationBar.tintColor = tintColor
    navigationBar.titleTextAttributes = [
      .font: UIFont.preferredFont(forTextStyle: .headline),
      .foregroundColor: textColor
    ]

    if #available(iOS 11.0, *) {
      navigationBar.prefersLargeTitles = prefersLargeTitles
      navigationBar.largeTitleTextAttributes = [
        .font: UIFont.preferredFont(forTextStyle: .largeTitle),
        .foregroundColor: textColor
      ]
    }

    if #available(iOS 13.0, *) {
      let navBarAppearance = UINavigationBarAppearance()
      navBarAppearance.configureWithOpaqueBackground()
      navBarAppearance.titleTextAttributes = navigationBar.titleTextAttributes ?? [:]
      navBarAppearance.largeTitleTextAttributes = navigationBar.largeTitleTextAttributes ?? [:]
      navBarAppearance.backgroundColor = barTintColor
      navBarAppearance.shadowColor = barTintColor
      navigationBar.standardAppearance = navBarAppearance
      navigationBar.scrollEdgeAppearance = navBarAppearance
    }

  }

}

1

1 Answers

5
votes

I had the same problem. Tried everything to fix it. The result is that this is definitely iOS 13 bug because even system apps like Notes does the same. Let's hope Apple will fix it with next iOS update.

Bellow are screenshots from Notes app.

Before tapping on cancel button in search bar

Step 1

After tapping cancel

Step 2

Update. iOS 13.2 is out and this bug seems to be fixed ✅