UISearchBar使用记录

本文介绍如何自定义UISearchBar的UI样式,并解决了当UINavigationController隐藏时点击searchBar无法聚焦并触发移动动画的问题。文章详细说明了通过修改UISearchBar内部组件实现自定义样式的方法。
  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是由TextFieldsubviews组成,如要进行自定义UI,可以获取其subView设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值