UINavigationBar turns white and won't close when using navigationItem.searchController in iOS 13

自闭症网瘾萝莉.ら 提交于 2020-01-06 08:12:35

问题


I am having trouble using navigationItem.searchController in iOS 13. The search bar of the search controller is initially closed, but it won't collapse back after I scroll the table view. It 'bounces' back when I try to hide the bar.

Also, the navigation bar turns white after the search bar appears. (The navigation bar has a custom tint color; see the attached screenshots.) Note that this wasn't the case in iOS < 13.

Is this a bug in iOS 13? If it is, is there a work around to keep the tint color of the navigation bar and make the search bar hide when the table view is scrolled upwards?

The search bar seems to hide when the table view is populated with cells to fill the screen.

The following is the code for my ViewController, which is embedded in a UINavigationController:

class ViewController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.searchController = UISearchController(searchResultsController: nil)
  }
}

Screenshots:

EDIT: I managed to 'fix' the new behavior using the new UINavigationBarAppearance API, but I still cannot get the original translucent look of the navigation bar and making it collapse.

UINavigationBar.appearance().isTranslucent = true doesn't seem to work along with the new API, so I just adjusted the color to match the translucent version using a color picker.

Also, there does not seem to be a way to collapse the search bar properly.

class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
    -> Bool
  {
    if #available(iOS 13.0, *) {
      let appearance = UINavigationBarAppearance()
      appearance.configureWithOpaqueBackground()
      appearance.backgroundColor = .redAdjusted  // adjusted to match the translucent version
      appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
      appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

      UINavigationBar.appearance().standardAppearance = appearance
      UINavigationBar.appearance().scrollEdgeAppearance = appearance
    } else {
      UINavigationBar.appearance().tintColor = .white
      UINavigationBar.appearance().barTintColor = .red
      UINavigationBar.appearance().isTranslucent = true
    }
  }
}

来源:https://stackoverflow.com/questions/58780350/uinavigationbar-turns-white-and-wont-close-when-using-navigationitem-searchcont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!