[英]How to clear UISearchBar background color in iOS10 Swift?
由於 UISearchBar 中的布局更改,此問題的舊版本不再適用於舊版本的 iOS。
我嘗試了以下方法來完全刪除 UISearchBar 的背景顏色,但它不起作用。 (我試圖查找它的層次結構視圖,但 xCode 一直為此選項崩潰。)
private func clearBackgroundColor() {
for view in self.subviews {
view.backgroundColor = UIColor.clear
for subview in view.subviews {
subview.backgroundColor = UIColor.clear
}
}
}
有什么想法或建議嗎?
謝謝!!
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
}
}
}
這就是我最終所做的工作。 謝謝大家的回答。
我想你提到了搜索欄的 BarTintColor
嘗試這個:
searchBar.barTintColor = .white
嘗試這個。
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
}
}
}
嘗試設置 backgroundimage: [searchBar setBackgroundImage:[UIImage new]];
let searchBar = UISearchBar()
searchBar.backgroundImage = UIImage()
您可以在界面生成器中將搜索樣式更改為“最小”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.