How to change textColor of Cancel button of UISearchBar in iOS7?
Asked Answered
B

17

40

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.

enter image description here

How can i change it?

Blowing answered 6/10, 2013 at 8:9 Comment(1)
Swift 4.2, 4.0+, I have added an answer for multiple customizations including cancelButton for searchBar here stackoverflow.com/questions/51345642Saddler
B
56

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)
Blowing answered 6/10, 2013 at 8:21 Comment(5)
UITextAttributeTextColor etc. are deprecated in iOS7. Use NSForegroundColorAttributeName etc. instead.Annotate
I don't know if this worked before but as of 7.0.3 it DOES NOT work.Catabolism
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];Parham
You can simply change the tint color!Vizierate
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: UIControlState.normal) Really helpful answer...!!Toot
S
16

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]];
Salted answered 8/1, 2014 at 12:57 Comment(0)
O
12

You can do it like this

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
Oeflein answered 20/7, 2014 at 7:58 Comment(1)
The problem with this, is that it also sets the cursor color.Us
V
12

A much simpler way -->

self.searchbar.tintColor = [UIColor darkGrayColor];
Vizierate answered 7/3, 2016 at 23:11 Comment(0)
S
12

Updated for 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

Worked for SWIFT 4

Use the 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

enter image description here

Sufficiency answered 14/8, 2017 at 6:33 Comment(1)
Glad i could help :)Sufficiency
F
9

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];
    }
}
Fiat answered 11/10, 2013 at 3:42 Comment(1)
Digging through subviews is fragile way of doing this. Use the appropriate UIAppearance "when contained in" methods.Jellybean
D
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.

Disannul answered 10/7, 2014 at 11:45 Comment(1)
This is deprecated in iOS 9. "Use +appearanceWhenContainedInInstancesOfClasses: instead" from framework.Carpi
E
4

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)
Electoral answered 9/8, 2017 at 22:17 Comment(0)
P
2
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
Plumper answered 18/6, 2015 at 11:47 Comment(0)
C
2

For swift 4.2

let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
Covey answered 2/1, 2019 at 18:26 Comment(0)
M
1

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.

Morrissey answered 5/1, 2017 at 9:37 Comment(0)
P
1

For Swift 3:

self.searchController.searchBar.tintColor = UIColor.white
Pulmonary answered 19/5, 2017 at 22:54 Comment(0)
H
1

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)
Haemal answered 13/3, 2018 at 5:20 Comment(0)
I
0

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()
Isaac answered 14/9, 2016 at 1:44 Comment(0)
D
0

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];
Doleful answered 28/9, 2016 at 9:55 Comment(0)
K
0

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.

Krein answered 25/11, 2016 at 12:34 Comment(0)
C
0

Swift 4

let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
Cleave answered 22/2, 2018 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.