UIKit - How to prevent default blur and focus background of UITextField?
Actually we always can inherit UIKit classes and do whatever layout/style we want. Here is very raw demo of how this can be done.
Prepared & tested with Xcode 12.1 / tvOS 14.0
So just substitute custom subclass of UITextField class
class MyTextField: UITextField {
lazy var textLayer = CATextLayer()
override func didMoveToSuperview() {
super.didMoveToSuperview()
layer.backgroundColor = UIColor.clear.cgColor
textLayer.font = self.font
textLayer.fontSize = 36
textLayer.foregroundColor = UIColor.white.cgColor
textLayer.alignmentMode = .center
textLayer.frame = layer.bounds
layer.addSublayer(textLayer)
layer.borderWidth = 2
}
override func layoutSublayers(of layer: CALayer) {
layer.borderColor = self.isFocused ? UIColor.black.cgColor : UIColor.clear.cgColor
textLayer.frame = layer.bounds
textLayer.string = self.text?.isEmpty ?? true ? self.placeholder : self.text
}
override func addSubview(_ view: UIView) {
// blocks standard styling
}
}
tvOS UITextField blank after editing
The reason why it isn't working is because the UITextField
is using a non-default background color. Apparently in tvOS, the background color is rendered to the layer after the text has been rendered (Interestingly enough, this does not affect placeholder text). This also happens in interface builder. A bug report has been sent to Apple.
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 not rendering background color
When textfield is in stackView call
self.field1.isHidden = false
self.field1.setNeedsDisplay()
search bar text field background color swift
You can simply set the barTintColor
of the UISearchBar
instance to your required color, i.e.
searchBarOutlet.barTintColor = .white
And use backgroundColor
change the background color of the searchTextField
searchBarOutlet.searchTextField.backgroundColor = .white
So, the below code works fine as per your requirement
searchBarOutlet.layer.cornerRadius = 6
searchBarOutlet.layer.masksToBounds = true
searchBarOutlet.layer.borderWidth = 1.5
searchBarOutlet.layer.borderColor = UIColor.init(hexFromString: "7E5BEB").cgColor
searchBarOutlet.isTranslucent = true
searchBarOutlet.barTintColor = .white
searchBarOutlet.searchTextField.backgroundColor = .white
Screenshot:
Related Topics
How to Save a Custom Class as an Attribute of a Coredata Entity in Swift 3
Hide View Item of Nsstackview with Animation
How to Segue Values When My Viewcontroller Is Embedded in an Uinavigationcontroller
Saving Highscores with Nsuserdefaults
Swiftui How to Animate Each Character in Textfield
Making Button Span Across VStack
Does Swift Have Short-Circuiting Higher-Order Functions Like Any or All
Charts Not Plotting in Tableviewcell
Using Foreach with a String Array - [String] Has No Member 'Identified'
Understanding Singleton in Swift
Scrolling Delegate in Tableview
Integrating Congnito User Pools with Amazon Cognito Identity
Expression Pattern of Type 'String' Cannot Match Values of Type 'Nsstoryboardsegue.Identifier
Cannot Call Value of Non-Function Type 'Nshttpurlresponse' Alamofire Objectmapper