D.M
D.M

Reputation: 530

Color not set properly to search bar

I'm setting up my search bar like so...

    self.searchBar.placeholder = "Search Plants"
    searchBar.layer.cornerRadius = 5
    searchBar.clipsToBounds = true
    searchBar.barTintColor = UIColor(red:255/255, green:127/255, blue:0, alpha:1)
    
    
    if let textField = self.searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }).first {
        textField.subviews.first?.isHidden = true
        textField.layer.backgroundColor = UIColor.white.cgColor
         
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
    }

Now when I run the project in an iPhone 7, it looks properly like so...

enter image description here

But when I run it in an iPhone XR, it looks like so...

enter image description here

What could be the reason for this...?

Upvotes: 1

Views: 81

Answers (1)

staticVoidMan
staticVoidMan

Reputation: 20234

The searchBar.searchTextField by default has a slightly opaque background color and so to prevent bleeding of the tintColor, we will need to explicitly set the color.

searchBar.searchTextField.backgroundColor = .white

Upvotes: 2

Related Questions