iOS 13: Отключить UISearchController для элемента навигации вызывает возврат tintColor обратно к значению по умолчанию - PullRequest
2 голосов
/ 16 октября 2019

Я использую UISearchController для элемента навигации (панель навигации настроена для использования barTintColor и tintColor). Когда контроллер поиска закрывается (при нажатии кнопки «Отмена» на панели поиска), цвет кнопки tintColor кнопки возврата возвращается к значению по умолчанию (синему).

Снимок экрана 1. Значение цвета tintColor для панели навигации равно .white enter image description here

Снимок экрана 2: Когда активен UISearchController: enter image description here

Снимок экрана 3: Когда UISearchController отключен (при нажатии кнопки Отмена) - вы могли видеть, что оттенок цвета для кнопки «Назад» возвращается к значению по умолчанию (синему), а не к белому: enter image description here

Код на снимках экрана:


/// 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 Ответ

5 голосов
/ 18 октября 2019

У меня была такая же проблема. Перепробовал все, чтобы это исправить. В результате это определенно ошибка iOS 13 , потому что даже системные приложения, такие как Notes, делают то же самое. Будем надеяться, что Apple исправит это в следующем обновлении iOS.

Ниже приведены скриншоты из приложения Notes.

Перед нажатием кнопки отмены в строке поиска

Step 1

После нажатия отменить

Step 2

Обновить. iOS 13.2 вышла, и эта ошибка, кажется, исправлена ​​✅

...