如何将UISearchBar添加到UIView?

时间:2014-04-04 14:42:37

标签: ios ios7 uisearchbar uisearchdisplaycontroller

我想向UISearchBar而不是UIView添加UIViewController,问题是UISearchDisplayController的init方法需要UIViewController作为contentsController: UISearchBar。 我可以看到 self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; // this was my approach, but this DOES NOT work UIViewController *results = [[UIViewController alloc] init]; results.view.userInteractionEnabled = NO; [self insertSubview:results.view aboveSubview:self.menuList]; self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:results]; self.searchController.delegate = self; self.searchController.searchResultsDataSource = self; self.searchController.searchResultsDelegate = self; self.tableView.tableHeaderView = self.searchBar; 在视图中正确显示,但如果我点击它,则栏会消失。这是我目前正在使用的代码:

{{1}}

在:

点击进入搜索栏后:

1 个答案:

答案 0 :(得分:0)

在swift中,

我使用hidesNavigationBarDuringPresentation来解决此问题。

此问题似乎在点击搜索栏后,它会自动隐藏栏。单击视图外部后会很好。

我添加这一行:

self.searchController?.hidesNavigationBarDuringPresentation = false;

希望它有效

相关问题