这里的技巧是重写 UISearchDisplayController。它实际上只做 3 件事。
- 将搜索栏向上移动到视图顶部并隐藏 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];
}