How to change textColor of Cancel button of UISear

2019-01-13 18:18发布

问题:

I need to change the color of Cancel button text of UISearchBar in iOS7.

Normally UISearchBar Cancel button textColor is blue and I want to change textColor to redColor.

How can i change it?

回答1:

I found answers for my own questions.

Here is code , add in AppDelegate if you want to change all cancel button.

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                                  [UIColor redColor],
                                                                                                  UITextAttributeTextColor,
                                                                                                  [UIColor whiteColor],
                                                                                                  UITextAttributeTextShadowColor,
                                                                                                  [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
                                                                                                  UITextAttributeTextShadowOffset,
                                                                                                  nil]
                                                                                        forState:UIControlStateNormal];

Swift:

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


回答2:

If you only want to set the text color of the button, you only need one line:

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


回答3:

You can do it like this

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];


回答4:

A much simpler way -->

self.searchbar.tintColor = [UIColor darkGrayColor];


回答5:

You can change the subviews of the UISearchBar like this in - (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];
    }
}


回答6:

You can format your searchbar cancel button as follows

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

Hopes it works for you.



回答7:

Worked for SWIFT 4

Use appearance function of UIAppearance module -

Method 1:- Show cancel button on load with searchBar -

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

or -

Method 2:- Show cancel button color after searchBar clicked -

  UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red



回答8:

Swift 3:

Use this code to set Red color (text, courser, button)

searchController.searchBar.tintColor = .red

if you want to change cancel button color to white add this code too

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


回答9:

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


回答10:

My approach to set the cursor and the button color independently is this: I set the cursor color to blue in the App Delegate (-application:didFinishLaunchingWithOptions:):

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

And then use the search bar's tint color in each controller to set the buttons color. You can set the tint color even on the Storyboard.



回答11:

For Swift 3:

self.searchController.searchBar.tintColor = UIColor.white


回答12:

Tested on 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)


回答13:

For people using Swift, I had to first add Eddie K's extension

Then I was able to call it like so (basically what Sabo did in the accepted answer):

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


回答14:

UITextAttribute is depricated from IOS 7 use below for IOS 7 or later

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


回答15:

An alternative to the UISearchBar would be the SHSearchBar. This swift framework is easily customizable without hacking, open source, has a lot of unit tests and is available via Cocoapods. It needs at least iOS 8.



回答16:

Swift 4

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


回答17:

For swift 4.2

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