Fir*_*ist 37 textcolor uisearchbar ios7
我需要更改UISearchBariOS7 中取消按钮文本的颜色.
通常UISearchBar取消按钮textColor是蓝色,我想要更改textColor为redColor.

我怎么能改变它?
Fir*_*ist 55
我找到了自己问题的答案.
这是代码,AppDelegate如果要更改所有取消按钮,请添加.
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
迅速:
let attributes = [NSForegroundColorAttributeName : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
Run Code Online (Sandbox Code Playgroud)
s1m*_*m0n 16
如果您只想设置按钮的文本颜色,则只需要一行:
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
小智 12
你可以这样做
[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
Run Code Online (Sandbox Code Playgroud)
Kak*_*hah 12
一种更简单的方式 - >
self.searchbar.tintColor = [UIColor darkGrayColor];
Run Code Online (Sandbox Code Playgroud)
您可以更改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];
}
}
Run Code Online (Sandbox Code Playgroud)
为**工作 Swift 5
使用模块SWIFT 4功能appearance-
方法1: -加载时显示取消按钮UIAppearance-
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
Run Code Online (Sandbox Code Playgroud)
要么 -
方法2: - searchBar点击后显示取消按钮颜色-
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式格式化搜索栏取消按钮
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"Your Text Here"];
Run Code Online (Sandbox Code Playgroud)
希望它对你有用.