使用UITextField和UISearchDisplayController代替UISearchBar

Dom*_*man 1 iphone uitextfield uisearchbar uisearchdisplaycontroller ios

如何在UISearchDisplay控制器中使用文本字段而不是搜索栏?

我想通过摆脱放大镜图标和自定义背景来完全自定义搜索栏.同时停止调整大小并调出"取消"按钮.我看到一些人使用hacky方法通过编辑不应该编辑的搜索栏api的部分来做到这一点.因此,在这个级别上进行自定义的更可接受的方式似乎是使用UITextfield而不是UISearchBar.但网上似乎没有关于这样做的任何信息!

如果我使用文本字段,当文本更改以使UISearchDisplayController工作时,我需要调用哪些方法?

And*_*mer 5

这里的技巧是重写UISearchDisplayController.它真的只做了三件事.

  1. 将搜索栏向上移动到视图顶部并隐藏UINavigationBar.
  2. 在视图的其余部分放置透明封面视图.
  3. 显示任何搜索结果的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];
}
Run Code Online (Sandbox Code Playgroud)