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?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109
  • `Swift 4.2, 4.0+`, I have added an answer for multiple customizations including cancelButton for searchBar here https://stackoverflow.com/questions/51345642 – Kamran Jul 16 '18 at 07:06

17 Answers17

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)
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
Fire Fist
  • 7,032
  • 12
  • 63
  • 109
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]];
s1m0n
  • 7,825
  • 1
  • 32
  • 45
12

You can do it like this

[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
HipHopCoder
  • 129
  • 1
  • 2
12

A much simpler way -->

self.searchbar.tintColor = [UIColor darkGrayColor];
Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31
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

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Jack
  • 13,571
  • 6
  • 76
  • 98
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];
    }
}
James Zaghini
  • 3,895
  • 4
  • 45
  • 61
morisunshine
  • 780
  • 4
  • 7
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.

Bhoopi
  • 6,523
  • 3
  • 22
  • 16
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)
Beslan Tularov
  • 3,111
  • 1
  • 21
  • 34
2
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
pableiros
  • 14,932
  • 12
  • 99
  • 105
jkyin
  • 168
  • 1
  • 12
2

For swift 4.2

let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
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.

gklka
  • 2,459
  • 1
  • 26
  • 53
1

For Swift 3:

self.searchController.searchBar.tintColor = UIColor.white
pableiros
  • 14,932
  • 12
  • 99
  • 105
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)
Den
  • 3,179
  • 29
  • 26
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()
Community
  • 1
  • 1
ghostatron
  • 2,620
  • 23
  • 27
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];
iOS Developer
  • 437
  • 4
  • 11
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.

blackjacx
  • 9,011
  • 7
  • 45
  • 56
0

Swift 4

let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
Yifan
  • 1,185
  • 12
  • 18