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




(12)

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

通常UISearchBar取消按钮textColor是蓝色,我想将textColor更改为redColor

我怎么能改变它?


UISearchBar的替代品是SHSearchBar 。 这个快速框架很容易定制,没有黑客攻击,开源,有很多单元测试,可通过Cocoapods 。 它至少需要iOS 8。


斯威夫特3:

使用此代码设置红色(文本,courser,按钮)

searchController.searchBar.tintColor = .red

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

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)

你可以这样做

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];

在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)

对于IOS 7或更高版本,UITextAttribute从以下IOS 7使用中删除

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:17]} forState:UIControlStateNormal];

对于Swift 3:

self.searchController.searchBar.tintColor = UIColor.white

对于使用Swift的人,我不得不首先添加Eddie K的扩展名

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

UIBarButtonItem.appearanceWhenContainedWithin(UISearchBar.self).setTitleTextAttributes()

您可以在- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar更改UISearchBar的子视图

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];
    }
}

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

这是代码,如果要更改所有取消按钮,请在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 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

使用UIAppearance模块的appearance功能 -

方法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 appearance] setTintColor:[UIColor redColor]];






textcolor