1

I am using UISearchBar, but i am getting problem in cancel Button in iOS 6, which works fine in iOS 7

FOR IOS 6 For iOS 6

FR IOS 7For iOS 7

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:whiteColor];

In iOS 7, Button's border doesn't shows up. Only "Cancel" is written with white color. In iOS 6, Button with its border appears with whole background also white. Please help.

Himanshu Garg
  • 599
  • 9
  • 21

1 Answers1

0

If I understand you correctly, you want to make the button's background transparent and have no border. If so, I think I found a solution:

For iOS6:
You have to set the backgroundImage property to image with a transparent background. For example you can do it with an UIImage object made with [UIColor clearColor].

For iOS7:
Just change nothing, but tintColor :)


How to do it?

Firstly, I used a method from here: LINK, but changed it a little bit to:

- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}


Then in viewDidLoad I wrote:

if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 7.0) {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundImage:[self imageWithColor:[UIColor clearColor] andSize:CGSizeMake(30.f, 40.f)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    } else {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
    }


I hope it will solve your problem :)

Community
  • 1
  • 1
bevoy
  • 751
  • 4
  • 14