how to change uitextfield color in searchcontroller?

前端 未结 1 1075
悲&欢浪女
悲&欢浪女 2021-01-21 05:56

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

1条回答
  •  借酒劲吻你
    2021-01-21 06:48

    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

    0 讨论(0)
提交回复
热议问题