iOS 设置自带UISearchBar的背景颜色(并改为圆角)

/** 取消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;

}

然后在viewdidload里面

/** 设置背景图片 */

[_searchBar setBackgroundImage:[UIImage imageNamed:@"red_nav_bar"]];   //这里图片的颜色和导航栏一样

/** 设置背景色 */

[_searchBar setBackgroundColor:[UIColor redColor]];

/** 设置文本框背景 *

[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"imageView"] forState:UIControlStateNormal];

//更改search圆角

UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

if (searchField) {

[searchField setBackgroundColor:[UIColor whiteColor]];

searchField.layer.cornerRadius = 14.0f;

searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;

searchField.layer.borderWidth = 1;

searchField.layer.masksToBounds = YES;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,144评论 1 6
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,573评论 6 30
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 838评论 0 0
  • 导读 记录直播平台从建立到衰竭,有幸目睹,并建立一套关于直播平台的方法论(是否适用于其他行业平台还有待验证) 平台...
    大班尼banny阅读 504评论 0 0
  • 你所想的穷苦不会无穷无尽的,你所担心的恐惧也不会一直都有。 昨晚,想着如果一直都没有钱交房租,没有钱吃饭了,我还活...
    柔软的时候阅读 176评论 0 0