iosswiftuinavigationcontrollerios11uisearchcontroller

how to change uitextfield color in searchcontroller?


enter image description here

i have embedded SearchController in navigation bar . How to Change UITextField Color The one which Holds search String To White .?


Solution

  • This will help you to achieve, what you want. Just apply your color combination in this code and see.

    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 = // Set text color
                    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 = UIColor.blue
                }
                navigationItem.searchController = sc
                navigationItem.hidesSearchBarWhenScrolling = false
    
    }
    

    Result:

    enter image description here