1、自訂UISearchBar的子類MySearchBar,重寫layoutSubviews方法。
代碼:
- (void)layoutSubviews {[super layoutSubviews];for (UIView *view in self.subviews){if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {for (UIView *subview in view.subviews) {if ([subview isKindOfClass:NSClassFromString(@"UITextField")]) {UITextField *textField = (UITextField *)subview;CGRect frame = textField.frame;frame.size.height = 38;textField.frame = frame;textField.font = [UIFont fontWithName:@"Helvetica" size:17.0f];break;}}break;}}}
2、如果是由UISearchBar是在一個xib檔案中定義的,則在awakeFromNib方法裡做如下操作:
for (UIView *view in self.destinationSearchBar.subviews){if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {[[view.subviews objectAtIndex:0] removeFromSuperview];CGRect frame = view.frame;CGFloat height = frame.size.height + 8;frame.size.height = height;view.frame = frame;view.clipsToBounds = NO;break;}}
如果是用alloc/init方法產生的,則在其對應的UViewController的viewDidLoad方法裡添加代碼