Ios – How to change cancel button tint color of UISearchBar in iOS7

iosiphoneobjective cuisearchbar

I want to change tint color of Textfield to blue color and cancel button tint color of UISearchBar to white color.

I am using below code.

for (UIView *subView in searchBar.subviews)
{
    for (UIView *ndLeveSubView in subView.subviews)
    {
        if([ndLeveSubView isKindOfClass:[UITextField class]])
        {
            [(UITextField *)subView setTintColor:[UIColor blueColor]];
        }
        else if ([ndLeveSubView isKindOfClass:[UIButton class]])
        {
            [(UIButton *)subView setTintColor:[UIColor whiteColor]];
        }
    }
}

But this changes both Textfield and cancel button's tint color to white. Can any one suggest another method for it?

Here's what I am getting…

enter image description here

The tint color of TextField is also White…..

Best Answer

Try some thing like this:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil] forState:UIControlStateNormal];
Related Topic