【发布时间】:2014-05-16 22:44:59
【问题描述】:
我想将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;
之前:
点击进入搜索栏后:
【问题讨论】:
-
能否请您添加屏幕截图
-
@James03 我已经编辑了我的答案。
-
尝试在
viewDidLoad:中添加这个self.edgesForExtendedLayout = UIRectEdgeNone; -
问题是,搜索栏在自定义 UIView 中而不是在 UIViewController 中。我很确定这会导致问题,但不幸的是我没有其他可能性,因为我的 UIView 是一个下拉菜单。
标签: ios ios7 uisearchbar uisearchdisplaycontroller