40

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

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

在此处输入图像描述

我怎样才能改变它?

4

17 回答 17

56

我找到了自己问题的答案。

这是代码,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)
于 2013-10-06T08:21:00.790 回答
16

如果只想设置按钮的文字颜色,只需要一行:

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];
于 2014-01-08T12:57:21.170 回答
12

你可以这样做

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
于 2014-07-20T07:58:54.203 回答
12

一个更简单的方法-->

self.searchbar.tintColor = [UIColor darkGrayColor];
于 2016-03-07T23:11:32.310 回答
12

更新为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

在此处输入图像描述

于 2017-08-14T06:33:34.150 回答
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];
    }
}
于 2013-10-11T03:42:21.637 回答
6

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

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"Your Text Here"];

希望它对你有用。

于 2014-07-10T11:45:20.890 回答
4

斯威夫特 3:

使用此代码设置红色(文本、课程、按钮)

searchController.searchBar.tintColor = .red

如果您想将取消按钮颜色更改为白色,请添加此代码

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
于 2017-08-09T22:17:30.803 回答
2
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
于 2015-06-18T11:47:51.583 回答
2

对于 Swift 4.2

let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
于 2019-01-02T18:26:09.870 回答
1

我独立设置光标和按钮颜色的方法是:我在 App Delegate 中将光标颜色设置为蓝色(-application:didFinishLaunchingWithOptions:):

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blueColor]];

然后使用每个控制器中搜索栏的色调颜色来设置按钮颜色。您甚至可以在情节提要上设置色调颜色。

于 2017-01-05T09:37:29.230 回答
1

在 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)
于 2018-03-13T05:20:05.737 回答
1

对于斯威夫特 3:

self.searchController.searchBar.tintColor = UIColor.white
于 2017-05-19T22:54:37.750 回答
0

UITextAttribute 已从 IOS 7 中弃用,适用于 IOS 7 或更高版本

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:17]} forState:UIControlStateNormal];
于 2016-09-28T09:55:36.547 回答
0

UISearchBar 的替代方案是SHSearchBar。这个 swift 框架无需黑客攻击即可轻松定制,开源,有大量单元测试,可通过Cocoapods 获得。它至少需要 iOS 8。

于 2016-11-25T12:34:45.070 回答
0

斯威夫特 4

let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
于 2018-02-22T11:13:57.330 回答
0

对于使用 Swift 的人,我必须先添加 Eddie K 的扩展

然后我可以这样称呼它(基本上是 Sabo 在接受的答案中所做的):

UIBarButtonItem.appearanceWhenContainedWithin(UISearchBar.self).setTitleTextAttributes()
于 2016-09-14T01:44:44.383 回答