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
  • 6,972
  • 12
  • 61
  • 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
  • 4,677
  • 1
  • 33
  • 59
Fire Fist
  • 6,972
  • 12
  • 61
  • 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,875
  • 1
  • 31
  • 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,086
  • 1
  • 15
  • 30
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,335
  • 4
  • 37
  • 38
Jack
  • 12,109
  • 4
  • 69
  • 95
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,837
  • 4
  • 43
  • 60
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,393
  • 3
  • 21
  • 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,071
  • 1
  • 19
  • 34
2
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
pableiros
  • 12,936
  • 11
  • 88
  • 94
jkyin
  • 158
  • 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,282
  • 1
  • 23
  • 51
1

For Swift 3:

self.searchController.searchBar.tintColor = UIColor.white
pableiros
  • 12,936
  • 11
  • 88
  • 94
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
  • 2,431
  • 24
  • 22
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,580
  • 21
  • 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
  • 439
  • 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
  • 7,448
  • 5
  • 42
  • 50
0

Swift 4

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