IOS开发代码分享之设置UISearchBar的背景颜色

2022-10-21,,,,

在项目开发中,我们经常要用到UISearchBar,在网上看到了很多关于去除掉他背景色的方法,都已经失效了,今天来分享一个正常使用的方法,希望能帮到大家

今天用到UISearchBar,之前网上提供的方法已经不能有效的去除掉它的背景色了,修改背景色方法如下:

mySearchBar.backgroundColor = RGBACOLOR(249,249,249,1);
    mySearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:mySearchBar.bounds.size];
 
//取消searchbar背景色
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
     
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
     
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
     
    return image;
}

 

您可能感兴趣的文章:

  • IOS中一段文字设置多种字体颜色代码
  • iOS设置UIButton文字显示位置和字体大小、颜色的方法
  • iOS应用开发中UIView添加边框颜色及设置圆角边框的方法
  • iOS 设置状态栏的背景颜色方法
  • IOS UITableView颜色设置的实例详解
  • 关于iOS中的各种颜色设置总结大全(推荐)

《IOS开发代码分享之设置UISearchBar的背景颜色.doc》

下载本文的Word格式文档,以方便收藏与打印。