iOS UISearchBar相关

1. 效果图 

iOS UISearchBar相关_第1张图片

2. new个searchBar

#pragma mark - Getter

- (UISearchBar *)mySearchBar {
    if (!_mySearchBar) {
        _mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        _mySearchBar.delegate = self;
        _mySearchBar.placeholder = @"请输入关键字";
        _mySearchBar.tintColor = kColorRed;
        _mySearchBar.returnKeyType = UIReturnKeySearch;

        //no addsubview
    }

    return _mySearchBar;
}

3. 定制

- (void)initView {
    self.mySearchBar.frame = CGRectMake(0, 38, UIScreenWidth-kMySize(70), 1);
    [self updateSearchBarSearchIcon:@"31"];
        
    self.navigationItem.titleView = self.mySearchBar;
    
    UIImageView *ivLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 38, 0, 1)];
    ivLine.image = [UIImage imageNamed:@"搜索输入底线"];
    ivLine.width = self.mySearchBar.frame.size.width;
    [self.navigationItem.titleView addSubview:ivLine];
}

- (void)updateSearchBarSearchIcon:(NSString *)iconName {    
    [[self.mySearchBar.subviews[0] subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UITextField class]]) {
            ((UITextField *)obj).font = kFontCNSize(14);
            ((UITextField *)obj).textColor = [UIColor whiteColor];
            ((UITextField *)obj).borderStyle = UITextBorderStyleNone;
            ((UITextField *)obj).clearButtonMode = UITextFieldViewModeWhileEditing;
            
            //编辑时根据传入参数隐藏图标
            if ([iconName isEqualToString:@"HideIcon"]) {
                ((UITextField *)obj).leftView.hidden = YES;
            } else {
                ((UITextField *)obj).leftView.hidden = NO;
                //((UITextField *)obj).leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
            }
            
            //((UITextField *)obj).rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"微信支付"]];
        }
        
        //删除searchBar的背景
        else if ([obj isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [obj removeFromSuperview];
        }
    }];
}

4. 编辑时或点击搜索按钮触发搜索

#pragma mark - UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [self updateSearchBarSearchIcon:@"HideIcon"];
    
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    if (searchBar.text.length == 0) {
        [self updateSearchBarSearchIcon:nil];
    }

    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.mySearchBar resignFirstResponder];
    [self searchLoadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length == 0) {
        self.vShow.hidden = NO;
        
        return;
    } 
    
    [self searchLoadData];
}

5. 触摸屏幕或滑动tableview隐藏键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.mySearchBar resignFirstResponder];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.mySearchBar resignFirstResponder];
}


你可能感兴趣的:(iOS UISearchBar相关)