How to clear UISearchBar background color in iOS10 Swift?

There is an older version of this question for older versions of iOS no longer working due to layout changes in the UISearchBar.

I tried the following to completely remove the background color of the UISearchBar, but it does not work. (I tried to view hierarchical views, but xCode continued to fail 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?

Thank!!

+4
source share
5 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 at the end that worked. Thank you all for your answers.

+10
source

I think you mention the BarTintColor search bar

try the following:

searchBar.barTintColor = .white
+4
source

.

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
        }
    }
} 
+2

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

+2

Just set the .Minimalsearch style to .Minimalin in Interface .Minimalor code, and the background will become transparent.

enter image description here

0
source

Source: https://habr.com/ru/post/1669653/


All Articles