Tvos Textfield Transparent Background

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

demo2

demo3

So just substitute custom subclass of UITextField class

demo1

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.

Sample Image

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

Sample Image

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:

Sample Image



Related Topics



Leave a reply



Submit