Navigation bar becomes white when a UISearchController is added to it

前端 未结 4 760
感情败类
感情败类 2020-12-14 23:50

When I add a UISearchController to a UINavigationItem from an UINavigationController; it becomes white when the view loads and changes to the color specified when the user c

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 00:17

    It looks like it is required to use the new UINavigationBarAppearance on iOS 13. Try to add this to your viewDidLoad:

    let appearance = UINavigationBarAppearance()
    appearance.backgroundColor = .systemRed
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    

    You will probably also want to set the searchField backgroundColor:

    let searchField = searchController.searchBar.searchTextField
    searchField.backgroundColor = .systemBackground
    

提交回复
热议问题