创建个类,继承自UISearchBar重写layoutSubviews便可以鸟!
- (void)layoutSubviews
{
UITextField *searchField;
for(id view in self.subviews)
{
if([view isKindOfClass:[UITextField class]])
{
searchField = view;
break;
}
}
if(!(searchField == nil))
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillRect(context, rect);
UIImage * bgImage = [[UIImage alloc] init];
bgImage = UIGraphicsGetImageFromCurrentImageContext();
searchField.background = bgImage;
searchField.placeholder = @"搜索返利商户";
searchField.borderStyle = UITextBorderStyleBezel;
UIImage *image = [UIImage imageNamed: @"esri.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
[searchField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];//新加代码,修改placeholder文字颜色,上截图时没这效果,测试可行
searchField.leftView = iView;
}
[super layoutSubviews];
}