【问题标题】:Navigation bar items disappear when using UISearchDisplayController displaysSearchBarInNavigationBar使用 UISearchDisplayController 时导航栏项目消失
【发布时间】:2015-03-04 07:31:57
【问题描述】:

如何在导航栏项目中嵌入搜索栏?

我使用了[searchBar sizeToFit],但没有使用displaysSearchBarInNavigationBar,导航栏在单击搜索栏时消失。

我在 UISearchDisplayController UISearchDisplayController hiding navigation bar 中尝试了答案

-(void)viewDidLayoutSubviews{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}

但它会导致UISearchDisplayController searchResultsTableView 显示在导航栏下方,同时隐藏搜索栏。

【问题讨论】:

  • 我使用here的答案解决了这个问题,即使用[tableView setFrame:CGRectMake(0, 42, 320, 380)];,但是现在它与其他iOS设备不兼容并且当搜索栏中有数据时取消按钮不起作用的问题。其他人有什么建议吗?
  • 我用CGRectMake CGRect s = self.searchDisplayController.searchBar.frame; CGRect newFrame = CGRectMake(0, s.size.height, s.size.width, s.size.height*[filteredList count]); tableView.frame = newFrame; 尝试了另一种方法,但取消按钮问题仍然存在,现在即使使用[tableview reload],实时搜索行也不会更新。

标签: ios7 uinavigationbar uisearchbar uisearchdisplaycontroller


【解决方案1】:

我尝试使用以下方法修改 resultsTableView 框架尺寸,而导航栏中没有搜索栏,即self.searchDisplayController.displaysSearchBarInNavigationBar = NO,但这会导致在编辑模式下取消问题。这是取自Moving SearchBar table view

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView{

    CGRect s = self.searchDisplayController.searchBar.frame;
    CGRect newFrame = CGRectMake(0,
                                 s.size.height,
                                 s.size.width,
                                 s.size.height*[filteredList count]);

    tableView.frame = newFrame;


}

然后我从this线程中找到了更好的解决方案

只需在viewDidLoad 中使用[self.searchDisplayController.navigationItem setLeftBarButtonItem: self.sidebarButton],它就解决了我的问题。现在剩下的是如何根据内容设置 searchResultsTableView 框架大小,但这是另一个问题。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多