Iphone navigationItem ios7中的自定义UISearchBar

Iphone navigationItem ios7中的自定义UISearchBar,iphone,ios7,styles,Iphone,Ios7,Styles,我有一个自定义的UISearchBar,我想在self.navigationItem.titleView中显示它。我可以把它放在那里,按照我想要的方式看,但我认为我如何将它与UISearchDisplayController连接有问题 以下是viewDidLoad中的UISearchBar和UISearchDisplayController: searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,200.0, 22.0

我有一个自定义的UISearchBar,我想在self.navigationItem.titleView中显示它。我可以把它放在那里,按照我想要的方式看,但我认为我如何将它与UISearchDisplayController连接有问题

以下是viewDidLoad中的UISearchBar和UISearchDisplayController:

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,200.0, 22.0)];
searchBar.barStyle = UISearchBarStyleDefault;
searchBar.showsCancelButton = NO;
searchBar.placeholder = @"Search";
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDelegate = self;
searchDisplayController.searchResultsDataSource = self;
此时,搜索功能实际上不起作用。如果我添加
[searchDisplayController setDisplaysSearchBarInNavigationBar:YES]
搜索功能现在可以工作了,但是它把我添加到
搜索栏中的所有样式都弄乱了

编辑:其他信息:看起来像
[self.searchDisplayController.searchResultsTableView reloadData]未被调用。我已实施以下措施:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
出于某种原因,如果我不调用
[searchDisplayController setDisplaysSearchBarInNavigationBar:YES]则不会重新加载数据。也许我缺少一种方法


搜索时,它看起来像是调用了
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
,并且
tableView==self.searchDisplayController.searchResultsTableView
为真,但它不会继续调用
-(UITableViewCell*)tableView:(UITableView*))tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
好吧,我认为让它工作的唯一方法是将UISearchBar嵌入到UIView中,我现在对它的样式很满意,尽管我已经能够使它完全相同。

下面就是为什么
setDisplaysSearchBarInNavigationBar
不能按我想要的方式工作的原因,它显式覆盖原始导航栏:当返回“是”以在导航栏中显示搜索栏时,系统将使用搜索显示控制器的navigationItem属性,并忽略searchContentsController视图控制器的导航项(如果已设置)。显示的搜索字段在导航栏中占据尽可能大的宽度。如果我创建一个新视图,将搜索栏添加为子视图,将标题视图设置为新视图,并将contentsController设置为self.navigationController,我可以使其工作,但新视图会完全扰乱搜索栏的显示。