1

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.....

4

4 回答 4

13

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];
于 2013-10-29T06:43:55.960 回答
4
UITextAttributeTextColor

Has been deprecated in the iOS 7 SDK. Use NSForegroundColorAttributeName instead. Like so:

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

If your UISearchBar is in a UINavigationBar - you can do this:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
     setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor whitecolor] ,NSForegroundColorAttributeName,
                             [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
于 2014-03-12T09:31:14.980 回答
0

Please include this line below in your method-didFinishLanchingWithOptions:

[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage imageNamed:@"Your image.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
于 2013-10-29T06:53:33.917 回答
0
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
于 2015-04-29T22:48:59.573 回答