0

I'm using a UISearchController in my application but I can't figure a way to customise it. I looked here in Stackoverflow but none of the confirmed answers worked for me. I tried looking in more places but nothing works. This is my code:

    import UIKit

class MainVC: UIViewController {
    

    lazy var mSearchBarController: UISearchController = {
        let mSearchBar = UISearchController(searchResultsController: nil)
        mSearchBar.searchBar.barStyle = .default
        mSearchBar.searchBar.placeholder = "enter city here"
        mSearchBar.searchBar.tintColor = UIColor.white
        
        return mSearchBar
    }()
    
    override func viewDidLayoutSubviews() {
        setupSearchBar()
    }
    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 80/255, green: 135/255, blue: 179/255, alpha: 1.0)
        setupNavBar()
        self.navigationItem.searchController = mSearchBarController
        
    }
    
    private func setupNavBar(){
        navigationItem.title = "Home"
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.hidesSearchBarWhenScrolling = true
    }
    
    private func setupSearchBar(){
        let mSearchTextField = mSearchBarController.searchBar.value(forKey: "searchField") as? UITextField
        mSearchTextField?.textColor = UIColor(red: 255/255, green: 245/255, blue: 139/255, alpha: 1.0)
        
        let mAttributedPlaceholder = NSMutableAttributedString(string: "enter city here", attributes: [NSAttributedString.Key.foregroundColor : UIColor(red: 255/255, green: 245/255, blue: 139/255, alpha: 1.0)])
        mSearchTextField?.attributedPlaceholder = mAttributedPlaceholder
        
        for view in mSearchBarController.searchBar.subviews {
            for subview in view.subviews {
                if let textField = subview as? UITextField {
                    textField.backgroundColor = UIColor.red
                    textField.textColor = UIColor.white
                }
            }
        }
    }
}

I can't figure a way to change the textColor of the searchBar nor the backgroundColor of it. This is what I get:

enter image description here

Community
  • 1
  • 1
John Doah
  • 1,839
  • 7
  • 25
  • 46
  • 1
    for textcolor you can use this it is working fine https://stackoverflow.com/questions/28499701/how-can-i-change-the-uisearchbar-search-text-color – Wings Oct 20 '18 at 12:15
  • Thanks, It worked! Any idea what to do about the background color? – John Doah Oct 20 '18 at 12:18
  • see the answer maybe it will also solve your background color problem – Wings Oct 20 '18 at 12:47

1 Answers1

0

See I use this simple code to change the textColor and backgroundColor of seacrhBar:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!


override func viewDidLoad() {
    super.viewDidLoad()


    for view in searchBar.subviews {
        for subview in view.subviews {
            if let textField = subview as? UITextField {
                textField.backgroundColor = UIColor.red
                textField.textColor = UIColor.white
            }
        }
    }
}

for reference you can check:-

How can I change the UISearchBar search text color? (for textcolor)

http://jitu1990.blogspot.com/2017/06/textcolor-and-backgroundcolor.html (for backgorund color)

Clashsoft
  • 11,553
  • 5
  • 40
  • 79
Wings
  • 2,398
  • 23
  • 46
  • You can use an optional cast (`as?`) instead of `isKind(of:)`. – Clashsoft Oct 20 '18 at 12:35
  • Thanks, but that didn't work for me. The searchBar stays exactly the same. – John Doah Oct 20 '18 at 12:55
  • can you show your whole code... See I am using searchBar not searchController – Wings Oct 20 '18 at 12:59
  • I added the whole class. – John Doah Oct 20 '18 at 13:05
  • delete this lines `let mSearchTextField = mSearchBarController.searchBa...` from your code and just drag a searchbar and make outlet and you don't need this whole code `lazy var mSearchBarController: UISearchController ` make it simple and easy – Wings Oct 20 '18 at 13:20
  • Yeah I know, but I'm trying to learn to program the UI and not use the storyboard. – John Doah Oct 20 '18 at 13:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182191/discussion-between-v-rohit-and-john-doah). – Wings Oct 20 '18 at 13:52