iOS 11 UISearchBar in UINavigationBar

后端 未结 7 1030
有刺的猬
有刺的猬 2020-12-04 17:15

I want to place a search bar in the new navigation bar with the new iOS 11 large titles. However, the color of the search bar is automatically applied by iOS and I can\'t ch

7条回答
  •  一生所求
    2020-12-04 17:22

    This should work for you

    func addSearchbar(){
            if #available(iOS 11.0, *) {
                let sc = UISearchController(searchResultsController: nil)
                let scb = sc.searchBar
                scb.tintColor = UIColor.white
    
                if let navigationbar = self.navigationController?.navigationBar {
                    //navigationbar.tintColor = UIColor.green
                    //navigationbar.backgroundColor = UIColor.yellow
                    navigationbar.barTintColor = UIColor.blue
                }
    
                navigationController?.navigationBar.tintColor = UIColor.green
                navigationItem.searchController = sc
                navigationItem.hidesSearchBarWhenScrolling = false
            }
    }
    


    Result:

提交回复
热议问题