创建类mySearchBar 继承于 UISearchBar
重写layoutSubviews
#import "mySearchBar.h"
@implementation mySearchBar
-(void)layoutSubviews
{
[super layoutSubviews];
// 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
self.barTintColor = [UIColor whiteColor];
UITextField * textField = [[[self.subviews firstObject] subviews] lastObject];
if([textField isKindOfClass:[UITextField class]]){
//里面textField按照searchBar的frame大小来设置
textField.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
}
@end
解决~
网友评论