I’m trying to customize the appearance of a UITextField inside a UISearchBar. Specifically, I want to change the background color and other style properties. I am using the following code to make some adjustments
func configureMarkerSearchBar(with placeholder: String) { backgroundColor = .clear tintColor = .clear searchTextField.layer.cornerRadius = CGFloat(SearchBarConstants.searchBarLayerRadius) searchTextField.layer.masksToBounds = true sizeToFit() if let textField = value(forKey: SearchBarConstants.searchFieldKey) as? UITextField { let attributedString = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor: SearchBarConstants.searchBarTextInputColor]) textField.attributedPlaceholder = attributedString textField.font = UIFont.regularDMSans(of: SearchBarConstants.textFieldFontSize) textField.layer.cornerRadius = SearchBarConstants.searchBarCornerRadius textField.translatesAutoresizingMaskIntoConstraints = false textField.backgroundColor = .clear textField.tintColor = .clear NSLayoutConstraint.activate([ textField.heightAnchor.constraint(equalToConstant: CGFloat(46)), textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0), textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0), textField.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0) ]) searchTextField.clipsToBounds = true if let searchIcon = UIImage(named: SearchBarConstants.iconSearch) { let imageView = UIImageView(image: searchIcon) searchIcon.withRenderingMode(.alwaysTemplate) searchIcon.withTintColor(SearchBarConstants.searchBarTextInputColor) let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: SearchBarConstants.searchIconOffset + searchIcon.size.width, height: searchIcon.size.height)) imageView.frame = CGRect(x: SearchBarConstants.searchIconOffset, y: 0, width: searchIcon.size.width, height: searchIcon.size.height) paddingView.addSubview(imageView) textField.leftView = paddingView textField.leftViewMode = .always } } }
The issue I’m facing is that whenever I try to access and modify the background view of the UITextField, it always defaults to tertiarySystemFillColor instead of the custom color I set.
Has anyone encountered this issue before? How can I successfully change the background color and ensure my customizations are applied properly?
Thank you!
Has anyone encountered this issue before? How can I successfully change the background color and ensure my customizations are applied properly?
