i have embedded SearchController in navigation bar . How to Change UITextField Color The one which Holds search String To White .?
how to change uitextfield color in searchcontroller?
Asked Answered
U want to change textfield background to green & search string white –
Werby
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:
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
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
© 2022 - 2024 — McMap. All rights reserved.