Can't set correct background color for UITextField in UISearchController

Can't set correct background color for UITextField in UISearchController



I use UISearchController im my project like here https://www.youtube.com/watch?v=h7caxpp3Xps
And I try to set textField background like in answer here:
iOS 11 customise search bar in navigation bar
If I use colors like blue, green, white, black all ok
But when I try to set another color I got result like



enter image description here



Also I have two _UISearchBarSearchFieldBackgroundView inside UISearchBarTextField and one of them is filled by another color
enter image description here



is it a bug of apple developers?



Code of viewController - enter link description here






stackoverflow.com/questions/48805553/… use this

– wings
Sep 17 '18 at 8:22






thats not work for me

– umni4ek
Sep 17 '18 at 9:41






stackoverflow.com/questions/47814179/… try this if again not working then tell

– wings
Sep 17 '18 at 9:50




3 Answers
3



Create class property in UIColor extension


extension UIColor

class var themeColor:UIColor
return UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)


OR

extension UIColor
static let themeColor = UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)

Usage

self.view.backgroundColor = UIColor.themeColor



original answer here






No, same result again

– umni4ek
Sep 17 '18 at 9:22






ok check the translucent to false and try again

– Mohmmad S
Sep 17 '18 at 9:23






searchController.searchBar.isTranslucent = false, but same =(

– umni4ek
Sep 17 '18 at 9:29







so when u use other colors it works ?

– Mohmmad S
Sep 17 '18 at 9:32






make sure you use the right color representation if so i know it sounds stupid to check but just double check, i had similar problem in the past and this pretty much solved i am running out of ideas

– Mohmmad S
Sep 17 '18 at 9:33



You can set tint color and background color of searchBar.



controller.searchBar.barTintColor = UIColor.appThemeColor() controller.searchBar.backgroundColor = UIColor.white


controller.searchBar.barTintColor = UIColor.appThemeColor()


controller.searchBar.backgroundColor = UIColor.white


controller.searchBar.tintColor = Color.theme.value



Try this code to customize other things also if you want


import UIKit

class ViewController: UIViewController

@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad()
super.viewDidLoad()

self.searchBar.customize()

// Do any additional setup after loading the view, typically from a nib.


override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.



extension UISearchBar
func customize()
self.backgroundColor = UIColor.clear
self.backgroundImage = UIImage()

for subView in self.subviews
for subsubView in subView.subviews
if let textField = subsubView as? UITextField
textField.clearButtonMode = .whileEditing
textField.backgroundColor = UIColor.red
textField.layer.cornerRadius = textField.bounds.size.height / 2
textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.black.cgColor
textField.layer.borderWidth = 1.0




self.setImage(UIImage(named: "searchfield")?.withRenderingMode(.alwaysTemplate), for: UISearchBarIcon.search, state: .normal)
self.setPositionAdjustment(UIOffset(horizontal: 10, vertical: 0), for: UISearchBarIcon.search)
self.searchTextPositionAdjustment = UIOffset(horizontal: 8, vertical: 0)




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế