如何在iOS7中更改UISearchBar的取消按钮的textColor?

Fir*_*ist 37 textcolor uisearchbar ios7

我需要更改UISearchBariOS7 中取消按钮文本的颜色.

通常UISearchBar取消按钮textColor是蓝色,我想要更改textColorredColor.

在此输入图像描述

我怎么能改变它?

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)

  • iOS7中不推荐使用UITextAttributeTextColor等.请改用NSForegroundColorAttributeName等. (11认同)
  • `[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];` (3认同)

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)

  • 这个问题是它还设置了光标颜色. (3认同)

Kak*_*hah 12

一种更简单的方式 - >

self.searchbar.tintColor = [UIColor darkGrayColor];
Run Code Online (Sandbox Code Playgroud)


mor*_*ine 9

您可以更改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)


Jac*_*ack 7

为**工作 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)

在此输入图像描述


Bho*_*opi 6

您可以按如下方式格式化搜索栏取消按钮

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"Your Text Here"];
Run Code Online (Sandbox Code Playgroud)

希望它对你有用.