UISearchBar *_listSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [Globle shareInstance].globleWidth, 44.0)];
me.backgroundColor = RGB(0xf6, 0xf6, 0xf6);
((UISearchBar *)me).tintColor = RGB(0xf5, 0x43, 0x43);
if(AtLeastIOS7) ((UISearchBar *)me).barTintColor = RGB(0xf5, 0x43, 0x43);
NSArray *subviewContainer = AtLeastIOS7?((UIView *)_listSearchBar.subviews[0]).subviews : _listSearchBar.subviews;
for (UIView *sub in subviewContainer) {
if ([sub isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
sub.alpha = 0.f;
}
if ([sub isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
sub.layer.borderWidth = 0.5f;
[sub setNightBlock:^(UIView *me,BOOL isDay){
me.backgroundColor = [UIColor whiteColor];
me.layer.borderColor = RGB(0xd6, 0xd6, 0xd6).CGColor;
if([me isKindOfClass:[UITextField class]]){
((UITextField *)me).textColor = RGB(0x3e, 0x3e, 0x3e);
((UITextField *)me).textAlignment = NSTextAlignmentLeft;
if(AtLeastIOS7)
((UITextField *)me).tintColor = RGB(0x3d, 0x7c, 0xca);
if(AtLeastIOS7)
((UITextField *)me).placeholder = @"搜索";
}
}];
}
}
_listSearchBar.delegate = self;
_listSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
_listSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
_listSearchBar.keyboardType = UIKeyboardTypeDefault;
[_listSearchBar setShowsScopeBar:NO];
_listSearchBar.showsCancelButton = NO;
_tableView.tableHeaderView = _listSearchBar;
UISearchBar一般用于作为tableHeaderView
searchbar点击聚焦的移动动画只有在UINavigationBar
显示时才有效,当前viewController隐藏NavigationBar时,点击searchBar无动画,不能置顶。
点击时UINavigationBar向上移动消失,searchBar移动置顶,搜索结束UINavigationBar向下移动显示,searchBar重回原始位置。
UISearchBar是由TextField 等subviews组成,如要进行自定义UI,可以获取其subView设置。
本文介绍如何自定义UISearchBar的UI样式,并解决了当UINavigationController隐藏时点击searchBar无法聚焦并触发移动动画的问题。文章详细说明了通过修改UISearchBar内部组件实现自定义样式的方法。
1905

被折叠的 条评论
为什么被折叠?



