如何在iOS 7中更改文本UISearchBar的取消按钮颜色?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7中更改文本UISearchBar的取消按钮颜色?相关的知识,希望对你有一定的参考价值。
我找到了自己问题的答案。
这是代码,如果要更改所有取消按钮,请添加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)
我独立设置光标和按钮颜色的方法是:我在App Delegate(-application:didFinishLaunchingWithOptions :)中将光标颜色设置为蓝色:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blueColor]];
然后使用每个控制器中的搜索栏色调颜色来设置按钮颜色。您甚至可以在故事板上设置色调颜色。
对于Swift 3:
self.searchController.searchBar.tintColor = UIColor.white
在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)
对于swift 4.2
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
对于使用Swift的人,我不得不首先添加Eddie K's extension
然后我就能这样称呼它(基本上是Sabo在接受的答案中所做的):
UIBarButtonItem.appearanceWhenContainedWithin(UISearchBar.self).setTitleTextAttributes()
对于IOS 7或更高版本,不推荐使用IOS 7以下的文本属性
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:17]} forState:UIControlStateNormal];
UISearchBar的另一种选择是SHSearchBar。这个快速框架很容易定制,没有黑客攻击,开源,有很多单元测试,可以通过Cocoapods获得。它至少需要iOS 8。
斯威夫特4
let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
如果您只想设置按钮的文本颜色,则只需要一行:
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];
你可以这样做
[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
一种更简单的方式 - >
self.searchbar.tintColor = [UIColor darkGrayColor];
您可以在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"];
希望它对你有用。
更新了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
斯威夫特3:
使用此代码设置红色(文本,courser,按钮)
searchController.searchBar.tintColor = .red
如果要将取消按钮颜色更改为白色,请添加此代码
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
以上是关于如何在iOS 7中更改文本UISearchBar的取消按钮颜色?的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 8 中更改 UISearchBar 取消按钮文本
在 iOS 7.1 中,UISearchBar 的文本和占位符属性不再起作用