IOS development code share sets the background color of UISearchBar

  • 2020-05-09 19:22:26
  • OfStack

Today, with the use of UISearchBar, the method previously provided on the Internet is no longer effective in removing its background color. The modification of the background color method is as follows:

mySearchBar.backgroundColor = RGBACOLOR(249,249,249,1);
    mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:mySearchBar.bounds.size];
// cancel searchbar The background color 
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    return image;


Related articles: