How to change background color of the text field in the UISearchController?
Here is a an example on how to set the textField
background.
class ViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.searchController.searchBar.subviews.forEach({ view in
view.subviews.forEach({ view in
if let view = view as? UITextField {
textField = view
}
})
})
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
if let bg = self.searchTextField?.subviews.first {
bg.backgroundColor = .green
bg.layer.cornerRadius = 10
bg.clipsToBounds = true
}
}
}
Result
UITextField backgroundColor in UISearchBar iOS 10+
After a long wait for an answer, I decided to try some experiments. I created a new project, dragged a search bar, connected it to the ViewController, added the UISearchBar extension, and set the color - exactly the same as I did in my main project.
Voila! It does set the background color as it should, and as it shows in the linked topic in the main question.
So, I put the projects side by side, opened the storyboard in each of them and started setting the properties of the searchBar, one at a time, until I could see why the bar is not being set like it should in the main project.
The very first thing I tried was, search style. My project has it set to "Minimal". I changed it to "Minimal" in the test project and noticed right away that the color was no longer set.
So, setting the "Search Style" property of the searchBar in the main project to "Default" allowed that background color to be set.
I would appreciate it greatly if anyone could comment and say why it is that I can't use "Minimal" if I want to customize the background color, or how I could make it work.
Uitextfield background color blurry?
I found a solution where you place a view behind the UITextfield, and make it transparent.
let v = UIView()
v.frame = CGRect(x: 30, y: 100, width: 180, height: 30)
let blurEffect = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = v.bounds
blurView.backgroundColor = .clear
v.addSubview(blurView)
let p = UITextField()
p.frame = CGRect(x: 0, y: 0, width: 180, height: 30)
p.layer.isOpaque = true
p.backgroundColor = .clear
v.addSubview(p)
self.view.backgroundColor = .red
self.view.addSubview(v)
This is an example of proposed solution, with background image instead of red color, to emphasize the blur effect
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background") ?? UIImage())
UITextField isn't white on iPhone simulator - Xcode 12.0.1
Starting in Xcode 12 you will need to set the text field's background color, as it now defaults to nil (clear). So as @aheze mentioned you'll need to set it to white manually with yourTextField.backgroundColor = UIColor.white
.
Related to this question UITextField backgroundColor set in Interface Builder is nil at runtime in iOS 14
Related Topics
Detect When a Webview Video Becomes Fullscreen on iOS8
How to Create Entitlement.Plist File in Xcode
Swiftui Datepicker Jumps Between Short and Medium Date Formats When Changing the Date
iOS Alamofire Stop All Requests
Launch Images in iOS 7 with Xcode 5
How to Fill a Bezier Path with Gradient Color
Making a Drop Down List Using Swift
Install iOS App Directly from Website - No Password, No Itunes, No Udid
Avaudioplayer Produces Lag Despite Preparetoplay() in Swift
Avqueueplayer Playback Without Gap and Freeze
iOS Static Framework with Resources Inside
iOS 6 Xcode 4.5 Unsupported Architecture Armv7S
How to Animate a Uibutton Between Two Png Images
Add Nsurlconnection Loading Process on Uiprogressview
Iphone, Obtaining a List of Countries in an Nsarray