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!!

2
  • Did you call this function in ViewDidLayoutSubviews() ?
    – Lumialxk
    Feb 14, 2017 at 3:22
  • @Lumialxk Nah, UISearchBar doesn't have this method. I called it from initialization.
    – RainCast
    Feb 14, 2017 at 22:04

7 Answers 7

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.

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.
    – JoeVictor
    Jun 24, 2017 at 18:57
  • @QuantumHoneybees for iOS 11 you will need to iterate in the content view. searchBar.subviews.first?.subviews instead of searchBar.subviews
    – btype
    Dec 18, 2017 at 9:58
  • 1
    @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. Aug 3, 2019 at 4:42
5

I think you are mention about BarTintColor of search bar

try this:

searchBar.barTintColor = .white
2
  • 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.
    – RainCast
    Feb 14, 2017 at 22:20
  • 1
    UIColor.clear show the color of the view behind your searchBar. So I think the view behind your searchBar's backgounrd color is black Feb 15, 2017 at 1:56
3

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

enter image description here

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
        }
    }
} 
1
  • Thanks, I went to another direction after my xCode "View Hierarchy" function works, basically checked out all the names inside the layout.
    – RainCast
    Feb 16, 2017 at 0:20
1

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

1
let searchBar = UISearchBar()
searchBar.backgroundImage = UIImage()
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.