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;
}
posted on 2016-10-11 11:36 yucaijiang 阅读(13009) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步