iOS更改UISearchBar里面的UITextField的frame以及去除UISearchBar的背景

来源:互联网 发布:流量推广软件 编辑:程序博客网 时间:2024/05/21 10:59

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方法里添加代码

0 0
原创粉丝点击