iOS 13:关闭导航项上的 UISearchController 会导致 tintColor 恢复为默认值

iOS 13: Dismiss UISearchController on navigation item causes the tintColor reversing back to default

我在导航项上使用 UISearchController(导航栏已设置为使用 barTintColor 和 tintColor)。当搜索控制器被关闭时(点击搜索栏的取消按钮),后退按钮的 tintColor 将恢复为默认值(蓝色)。

屏幕截图 1:导航栏的 tintColor 是 .white

屏幕截图 2:当 UISearchController 处于活动状态时:

屏幕截图 3:关闭 UISearchController 时(通过点击取消)- 您可以看到后退按钮的 tintColor 反转回默认值(蓝色),而不是白色:

截图上的代码是:


/// 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
    }

  }

}

我遇到了同样的问题。尝试了一切来修复它。结果是这肯定是 iOS 13 bug 因为即使像 Notes 这样的系统应用程序也是如此。希望 Apple 会在下一个 iOS 更新中修复它。

以下是备忘录应用程序的屏幕截图。

点击搜索栏中的取消按钮之前

点击取消后

更新。 iOS 13.2 已经发布,这个错误似乎已修复 ✅