Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear UISearchBar background color in iOS10 Swift?

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

like image 500
RainCast Avatar asked Feb 14 '17 03:02

RainCast


2 Answers

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.

like image 186
RainCast Avatar answered Oct 13 '22 02:10

RainCast


I think you are mention about BarTintColor of search bar

try this:

searchBar.barTintColor = .white
like image 25
Nguyễn Anh Việt Avatar answered Oct 13 '22 01:10

Nguyễn Anh Việt