how to change uitextfield color in searchcontroller?
Asked Answered
E

2

3

enter image description here

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

Edinburgh answered 14/12, 2017 at 13:8 Comment(1)
U want to change textfield background to green & search string whiteWerby
C
5

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

Countertype answered 14/12, 2017 at 13:19 Comment(4)
When i click Search it makes the screen blurr (Loose Focus) As seen in animation also . How to Fix That . Will BarTintColor Fix That ?Edinburgh
Oh, I didn't mark that. Give me sometime. Let me check it and provide your feasible solution.Countertype
searchController.obscuresBackgroundDuringPresentation = false Setting This Property Did The Trick .Edinburgh
You are really great - You are welcomed to update and improve this answer with your suggested input. :)Countertype
A
0

Excellent answer, in my novice opinion we could also try using the member of the UI Search bar called searchTextField which is ultimately a subclass of UITextField but we can get away without using

if let textfield = scb.value(forKey: "searchField") as? UITextField
//we could use 
let textField = scb.searchTextField
followed by the same changes made to the views layer, cornerRadius etc
Aufmann answered 14/8, 2022 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.