如何在iOS 7中更改文本UISearchBar的取消按钮颜色?

时间:2013-10-06 08:09:34

标签: ios7 uisearchbar textcolor

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

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

enter image description here

我该如何改变?

17 个答案:

答案 0 :(得分: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];

夫特:

let attributes = [NSForegroundColorAttributeName : UIColor.red]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)

答案 1 :(得分:16)

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

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];

答案 2 :(得分:12)

你可以这样做

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];

答案 3 :(得分:12)

更简单的方法 - >

awk -F '[- ]' '!seen[$6]++ {n++} END {print n}' file

答案 4 :(得分: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];
    }
}

答案 5 :(得分:6)

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

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

希望它适合你。

答案 6 :(得分:6)

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

enter image description here

答案 7 :(得分:3)

斯威夫特3:

  

使用此代码设置红色(文字,   courser,button)

searchController.searchBar.tintColor = .red
  

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

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

答案 8 :(得分:2)

[[UISearchBar appearance] setTintColor:[UIColor redColor]];

答案 9 :(得分:1)

快速4.2

let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)

答案 10 :(得分: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)

答案 11 :(得分:1)

对于Swift 3:

self.searchController.searchBar.tintColor = UIColor.white

答案 12 :(得分:1)

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

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

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

答案 13 :(得分:0)

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

答案 14 :(得分:0)

Swift 4

let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)

答案 15 :(得分:0)

UITextAttribute从IOS 7使用以下用于IOS 7或更高版本

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

答案 16 :(得分:0)

对于使用Swift的人,我必须先添加 Eddie K's extension

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

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