不废话,直接代码
UISearchBar *mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(40, _statusBarHeight+7, _widthView-60*2+5, 30)];
mySearchBar.placeholder = @"Search products";
mySearchBar.backgroundImage = [UIImage createImageWithColor:[UIColor colorWithRed:247.0/255 green:247.0/255 blue:247.0/255 alpha:1.0]];
mySearchBar.delegate = self;
[self.view addSubview:mySearchBar];
//此处是把颜色改成UIImage
+ (UIImage*) createImageWithColor:(UIColor*) color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}