// 创建搜索框
UISearchBar*searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(10,Is_iPhoneX_Top+22,KWIDTH-60,32)];
searchBar.placeholder=@"想知道点什么呢";
searchBar.delegate=self;
searchBar.layer.cornerRadius=16;
searchBar.layer.masksToBounds=YES;
searchBar.barTintColor=kColor(238,238,238);
//[searchBar.layer setBorderWidth:8];
//[searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];
//用颜色图片来替换背景图片
[searchBarsetSearchFieldBackgroundImage:[self imageFromColor:kColor(238, 238, 238) withSize:CGSizeMake(KWIDTH-60, 36)] forState:UIControlStateNormal]; //输入背景框
[selfaddSubview:searchBar];
// 将UIColor转换成图片
- (nullableUIImage*)imageFromColor: (nonnullUIColor*)color withSize:(CGSize)size {
CGRectrect =CGRectMake(0,0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
网友评论