Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change textColor of Cancel button of UISearchBar in iOS7?

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?

like image 846
Fire Fist Avatar asked Oct 06 '13 08:10

Fire Fist


2 Answers

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)
like image 149
Fire Fist Avatar answered Nov 02 '22 02:11

Fire Fist


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]];
like image 16
s1m0n Avatar answered Nov 02 '22 01:11

s1m0n