UISearchbar在很多时候都是和UISearchDisplayController一起使用,并且是添加在UITableView上面来显示搜索结果的,但是当我们不需要UISearchDisplayController和UITableView展示,只是需要一个UISearchbar和一个搜索界面的时候,我们就可以直接添加UISearchbar在需要搜索的界面上面,然后创建一个显示需要显示结果的UIView界面,当然并不是确切的就是UIView可以是任何一个显示界面。
实现的方法可以如下:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
// return NO to not become first responder
//htSearchView就是显示结果的界面
htSearchView = [[HTSearchView alloc] initWithFrame:CGRectMake(0, 100, 320, 480)];
//实现搜索时候的阴影的效果
htSearchView.backgroundColor = [UIColor colorWithRed:37/255 green:34/255 blue:34/255 alpha:0.8];
[self.view addSubview:htSearchView];
[searchBar resignFirstResponder];
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
htSearchView.hidden = YES;
[haiTaoSearchBar resignFirstResponder];
NSLog(@"%@",searchBar.text);
}