[英]UISearchBar cancel button color?
當我將 UISearchBar 放入 Interface Builder 中的視圖中,並將其樣式更改為 Black Opaque 時,取消按鈕會保持不合適的藍色/灰色並且不會變成黑色。
如何使取消按鈕變黑?
編輯:它確實像這樣工作:
// Assume a UISearchBar searchBar.
NSArray *subviews = [searchBar subviews];
// The index depends on how you configure the searchBar.
UIButton *cancelButton = [subviews objectAtIndex:3];
// Set the style to "normal" style.
[cancelButton setStyle:0];
但是setStyle:
方法來自一個私有框架,所以在向 Apple 提交應用程序時這可能是一個問題。
我使用了這樣的東西並與我一起工作:
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];
它將取消按鈕顏色更改為黑色。
更新的iOS 9.0,該方法appearanceWhenContainedIn
已過時,使用appearanceWhenContainedInInstancesOfClasses
代替:
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];
在 Swift 3 中:
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.black
您的解決方案的問題在於代碼假設 objectAtIndex:3 是取消按鈕。 這不僅會生成編譯器警告,而且如果您以編程方式顯示“取消”按鈕(例如使用[searchBar setShowsCancelButton:YES]
,則可能會導致應用程序崩潰。
一個更簡單的解決方案是在 ViewDidLoad() 中設置整個搜索欄的樣式,使用:
searchBar.tintColor = [UIColor colorWithWhite:0.3 alpha:1.0];
這會覆蓋在界面生成器中設置的樣式,但也會將取消按鈕的顏色更改為與整個欄的顏色相同(盡管不幸的是,它不允許您獨立設置取消按鈕的樣式。
試試這個看看:(我用 Swift 4.1 - Xcode 9.3-beta4測試了下面的代碼)
@IBOutlet weak var sbSearchBar: UISearchBar!
sbSearchBar.showsCancelButton = true
sbSearchBar.barTintColor = UIColor.blue
sbSearchBar.tintColor = UIColor.red
if let buttonItem = sbSearchBar.subviews.first?.subviews.last as? UIButton {
buttonItem.setTitleColor(UIColor.yellow, for: .normal)
}
在 Swift 4.2 中
let appearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "goldColor")!], for: .normal)
這對我有用。 謝謝@Tim Semple
對於 iOS 10:
UISearchBar.appearance().tintColor = UIColor.red //cancel button color
UISearchBar.appearance().barTintColor = UIColor.blue //background button color
這是上面針對 Swift 3 的Hossam Ghareeb 回答的更新版本:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.red
但是,如果已經在別處為 UIBarButtonItem 設置了外觀,則這不會覆蓋外觀。
例如,在我的導航欄控制器中,我不得不改變這一點:
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)
為此,上述解決方案可以正常工作:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self] ).setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)
我已經采用了Benjamin 的答案,並將其與安全Array
查找相結合,以生成一個簡短但安全的功能版本:
searchController.searchBar.tintColor = UIColor.whiteColor()
(searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
.filter({$0.isKindOfClass(UITextField)})
.map({$0.tintColor = .lightGrayColor()})
這會導致在鍵入灰色時將取消按鈕和光標着色為白色。 否則它會是白色的,因此看不到。 searchController
是UISearchController
類型的對象。 如果有人想在結果控制器中使用它,請將其替換為self
。
safe:
下標的實現是nkukushkin 的回答:
extension Array {
subscript(safe index: Int) -> T? {
return indices(self) ~= index ? self[index] : nil
}
}
let view: UIView = self.searchBar.subviews[0] as UIView
let subViewsArray = view.subviews
for subView: UIView in subViewsArray {
if let cancelButt = subView as? UIButton{
cancelButt.setTitleColor(UIColor.white, for: .normal)
}
}
這對我有用
提出了以下解決方案,它也適用於 iOS 13.0 和 iOS 12.4,必須適用於之前的版本,直到 iOS 9.0。 以下解決方案適用於:
對於目標 C:
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateDisabled];
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSBackgroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
上面的代碼還修復了 iOS 13 和 iPhone X 的 UI 問題。我將此代碼包含在我的AppDelegate.m類中的didFinishLaunchingWithOptions函數中,以便可以在整個應用程序中完成更改。
對於那些希望在 Swift 中重現相同行為的人:
override func viewWillAppear(animated: Bool) {
self.searchBar.tintColor = UIColor.whiteColor()
let view: UIView = self.searchBar.subviews[0] as! UIView
let subViewsArray = view.subviews
for (subView: UIView) in subViewsArray as! [UIView] {
println(subView)
if subView.isKindOfClass(UITextField){
subView.tintColor = UIColor.blueColor()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.