How to clear UISearchBar background color in iOS10 Swift?
Asked Answered
P

7

5

There are older version of this question for older versions of iOS no longer works due to the layout changes in UISearchBar.

I've tried the following to completely remove the background color of UISearchBar but it doesn't work. (I tried to look up the hierarchy views of it but xCode kept crashing for this option.)

private func clearBackgroundColor() {
    for view in self.subviews {
        view.backgroundColor = UIColor.clear
        for subview in view.subviews {
            subview.backgroundColor = UIColor.clear
        }
    }
} 

Any ideas or suggestions?

Thanks!!

Pug answered 14/2, 2017 at 3:11 Comment(2)
Did you call this function in ViewDidLayoutSubviews() ?Iridis
@Iridis Nah, UISearchBar doesn't have this method. I called it from initialization.Pug
P
13
private func clearBackgroundColor() {
    guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }

    for view in self.subviews {
        for subview in view.subviews where subview.isKind(of: UISearchBarBackground) {
            subview.alpha = 0
        }
    }
}

This is what I did in the end that worked. Thanks all for your answers.

Pug answered 16/2, 2017 at 0:19 Comment(3)
Just as a comment for future wanderers... This works on Xcode 9, Swift 3.0 (haven't worked with 4.0 yet, don't wanna break my production code) @Lumialxk's answer didn't work.Haversack
@QuantumHoneybees for iOS 11 you will need to iterate in the content view. searchBar.subviews.first?.subviews instead of searchBar.subviewsSorrel
@QuantumHoneybees this solution works for iOS 12.4 also. I dont know how you did it, it works fine when added as a extension to uiview.Suellensuelo
C
5

I think you are mention about BarTintColor of search bar

try this:

searchBar.barTintColor = .white
Common answered 14/2, 2017 at 4:20 Comment(2)
Thanks. But the thing is I need is to set it to UIColor.clear. After I set it to clear, it actually shows up as black on screen. I need everything inside this search bar to have a transparent color, only except for the text input.Pug
UIColor.clear show the color of the view behind your searchBar. So I think the view behind your searchBar's backgounrd color is blackFootlocker
E
3

Just set the Search Bar style to .Minimal in Interface Builder or code and the background will go transparent.

enter image description here

Edaedacious answered 5/6, 2019 at 8:16 Comment(0)
I
2

Try this.

class CustomSearchBar: UISearchBar {
    override func layoutSubviews() {
        super.layoutSubviews()
        clearBackgroundColor() // function in the question
    }
}

private func clearBackgroundColor() {
    for view in self.subviews {
        view.backgroundColor = UIColor.clear
        for subview in view.subviews {
            subview.backgroundColor = UIColor.clear
        }
    }
} 
Iridis answered 15/2, 2017 at 6:19 Comment(1)
Thanks, I went to another direction after my xCode "View Hierarchy" function works, basically checked out all the names inside the layout.Pug
T
1

try set backgroundimage: [searchBar setBackgroundImage:[UIImage new]];

Therapy answered 20/7, 2017 at 3:41 Comment(0)
I
1
let searchBar = UISearchBar()
searchBar.backgroundImage = UIImage()
Ileac answered 29/5, 2020 at 18:9 Comment(0)
O
0

You can change search style to "Minimal" in Interface Builder

Octa answered 30/7, 2020 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.