網(wǎng)上的好多都是比較老的方法,iOS8之前適用的兴枯。遂進(jìn)行各種百度搜索血淌,以下方法親測有效。
//更改輸入框背景色
UIImage * searchBarBg = [self GetImageWithColor:[UIColor clearColor] andHeight:36.0f];
[self.search setBackgroundImage:searchBarBg];
[self.search setBackgroundColor:LIGHT_GRAY1];
[self.search setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
//更改輸入框圓角
self.search.layer.cornerRadius = 4.0f;
self.search.layer.masksToBounds = YES;
//更改輸入框字號
UITextField *seachTextFild = (UITextField*)[self subViewOfClassName:@"UISearchBarTextField" view:self.search];
seachTextFild.font = [UIFont systemFontOfSize:14];
- (UIView*)subViewOfClassName:(NSString*)className view:(UIView *)view{
for (UIView* subView in view.subviews) {
NSLog(@"======%@",subView.class);
if ([NSStringFromClass(subView.class) isEqualToString:className]) {
return subView;
}
UIView* resultFound = [self subViewOfClassName:className view:subView];
if (resultFound) {
return resultFound;
}
}
return nil;
}
/**
* 生成圖片
*
* @param color 圖片顏色
* @param height 圖片高度
*
* @return 生成的圖片
*/
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
UIGraphicsBeginImageContext(r.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, r);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}