iOS 11 UISearchBar in UINavigationBar

后端 未结 7 1048
有刺的猬
有刺的猬 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:31

    Tested on ios 13

    Perfect white background on searchBar

    func setupSearchbarController(){
        if #available(iOS 11.0, *) {
            let sc = UISearchController(searchResultsController: nil)
            sc.delegate = self
            let scb = sc.searchBar
            scb.tintColor = UIColor.white
            scb.barTintColor = UIColor.white
    
            if let textfield = scb.value(forKey: "searchField") as? UITextField {
                textfield.textColor = UIColor.white
                textfield.backgroundColor = UIColor.white
                if let backgroundview = textfield.subviews.first {
    
                    // Background color
                    backgroundview.backgroundColor = UIColor.white
    
                    // Rounded corner
                    backgroundview.layer.cornerRadius = 10;
                    backgroundview.clipsToBounds = true;
                }
            }
    
            if let navigationbar = self.navigationController?.navigationBar {
                navigationbar.barTintColor = AppColor.themeColor
            }
            navigationItem.searchController = sc
            navigationItem.hidesSearchBarWhenScrolling = false
        }
    }
    

提交回复
热议问题