我需要更改UISearchBar
iOS7 中取消按钮文本的颜色。
通常UISearchBar
取消按钮textColor
是蓝色的,我想更改textColor
为redColor
.
我怎样才能改变它?
我需要更改UISearchBar
iOS7 中取消按钮文本的颜色。
通常UISearchBar
取消按钮textColor
是蓝色的,我想更改textColor
为redColor
.
我怎样才能改变它?
我找到了自己问题的答案。
这是代码,AppDelegate
如果要更改所有取消按钮,请添加。
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
迅速:
let attributes = [NSForegroundColorAttributeName : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
如果只想设置按钮的文字颜色,只需要一行:
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];
你可以这样做
[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
一个更简单的方法-->
self.searchbar.tintColor = [UIColor darkGrayColor];
更新为Swift 5
:
let searchBarCancelButtonForegroundColor = UIColor.red
let attributes = [NSAttributedString.Key.foregroundColor: searchBarCancelButtonForegroundColor]
// Regular mode
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
// After click
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = searchBarCancelButtonForegroundColor
为**工作SWIFT 4
使用模块的appearance
功能UIAppearance
-
方法1:-在加载时显示取消按钮searchBar
-
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
或者 -
方法 2: - 单击后显示取消按钮颜色searchBar
-
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
您可以更改UISearchBar
类似这样的子视图- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
UIView *view = [_searchBar.subviews objectAtIndex:0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton *)subView;
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
}
您可以按如下方式格式化您的搜索栏取消按钮
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"Your Text Here"];
希望它对你有用。
斯威夫特 3:
使用此代码设置红色(文本、课程、按钮)
searchController.searchBar.tintColor = .red
如果您想将取消按钮颜色更改为白色,请添加此代码
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
对于 Swift 4.2
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
我独立设置光标和按钮颜色的方法是:我在 App Delegate 中将光标颜色设置为蓝色(-application:didFinishLaunchingWithOptions:):
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blueColor]];
然后使用每个控制器中搜索栏的色调颜色来设置按钮颜色。您甚至可以在情节提要上设置色调颜色。
在 Swift 4 上测试
let barButtonItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
barButtonItem.title = NSLocalizedString("Cancel", comment: "")
barButtonItem.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 15.0, weight: .medium),
.foregroundColor : #colorLiteral(red: 0.1960784314, green: 0.1960784314, blue: 0.1960784314, alpha: 1)], for: .normal)
对于斯威夫特 3:
self.searchController.searchBar.tintColor = UIColor.white
UITextAttribute 已从 IOS 7 中弃用,适用于 IOS 7 或更高版本
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:17]} forState:UIControlStateNormal];
UISearchBar 的替代方案是SHSearchBar。这个 swift 框架无需黑客攻击即可轻松定制,开源,有大量单元测试,可通过Cocoapods 获得。它至少需要 iOS 8。
斯威夫特 4
let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
对于使用 Swift 的人,我必须先添加 Eddie K 的扩展
然后我可以这样称呼它(基本上是 Sabo 在接受的答案中所做的):
UIBarButtonItem.appearanceWhenContainedWithin(UISearchBar.self).setTitleTextAttributes()