首页 > 代码库 > UISearchBar
UISearchBar
1.搜索框常用代理方法
#pragma mark -搜索框的代理方法
#pragma mark 搜索框文字改变的代理方法
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
}
#pragma mark 搜索框开始编辑(开始聚焦)
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// 1.显示取消按钮
[searchBar setShowsCancelButton:YES animated:YES];
// 2.显示蒙板,_cover是uiview类型
if (_cover == nil) {
_cover = [[UIView alloc] init];
_cover.backgroundColor = [UIColor blackColor];
_cover.frame = _tableView.frame;
_cover.autoresizingMask = _tableView.autoresizingMask;
//蒙板的单击事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
[_cover addGestureRecognizer:tap];
}
[self.view addSubview:_cover];
_cover.alpha = 0.0;
[UIView animateWithDuration:0.3 animations:^{
_cover.alpha = 0.7;
}];
}
#pragma mark 点击取消按钮时回收蒙板以及取消按钮
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self coverClick];
}
#pragma mark 当退出搜索框的键盘时(失去焦点)
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[self coverClick];
}
UISearchBar