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?
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?
 
    
    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)
 
    
     
    
    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]];
 
    
    You can do it like this
[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
 
    
    A much simpler way -->
self.searchbar.tintColor = [UIColor darkGrayColor];
 
    
    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
 
    
     
    
    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];
    }
}
 
    
     
    
    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.
 
    
    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)
 
    
    For swift 4.2
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
 
    
    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.
 
    
    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)
 
    
    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()
 
    
     
    
    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];
 
    
    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.
 
    
    Swift 4
let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
