The color of a magnifying glass is being taken automatically from the background color of UITextView. Under the text view I have an image and the text view background itself
Subclass UITextView/UITextField
, override -drawRect
and do your own drawing, then you can use .backgroundColor
to set the magnifying glass tint.
It is possible for change colour of search magnify .
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor image:(UIImage *)image {
UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
// draw alpha-mask
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, image.CGImage);
// draw tint color, preserving alpha values of original image
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[tintColor setFill];
CGContextFillRect(context, rect);
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return coloredImage;
}
use this line when you pass image object to search bar.
UIImage *obj_image = [self tintedImageWithColor:[appDelegate.appDefaultdata valueForKey:@"d_colorA"] image:[UIImage imageNamed:@"search_magnify.png"]];
[contactSearchbar setImage:obj_image forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
This thread provides different solutions. I prefered the one labeled with "dirty hack", instead of the one marked as "solution".
As of iOS 5, you can use the following API:
UISearchBar.setImage(:forSearchBarIcon:state:)
It's easiest to just provide your own icon. If you like to tint it dynamically, you can provide your own icon and Core Graphics to create a new tinted version before setting it on the search bar.
At the time of writing, it was not possible on a non jailbroken iPhone.
Now, see the answer from @Parthpatel1105