这里的技巧是重写UISearchDisplayController。它只有三件事。
- 将搜索栏移动到视图顶部并隐藏UINavigationBar。
- 在视图的其余部分放置一个透明的遮盖视图。
- 用任何搜索结果显示UITableView。
所以注册您的UIViewController作为的UITextField的委托,并开始..
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//here is where you open the search.
//animate your textfield to y = 0.
//I usually make the search tableview and the cover a separate view,
//so I add them to my view here.
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *searchText = [[NSString alloc] initWithString:[textField.text stringByReplacingCharactersInRange:range withString:string]];
//display your search results in your table here.
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//hide all of your search stuff
[self.navigationController setNavigationBarHidden:NO animated:YES];
}