?// 創建搜索框
? ? ? ? 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;
}