我有一个tabbarcontroller,它位于uinavigationcontroller内部,我正在尝试将一个搜索栏放在其中一个选项卡上的tableviewheader中。我能够在导航控制器的任何屏幕上成功使用下面的代码,但当视图在tabbarcontroller上时,搜索不会响应触摸。我可以在代码中成功将其设置为活动,但我也需要触摸才能工作。
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[@"SEARCH"];
[self.searchController.searchBar setPlaceholder:@"SEARCH"];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.searchController.searchBar.backgroundColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:1];
self.searchController.searchBar.tintColor = [UIColor blackColor];
self.myTableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
答案 0 :(得分:1)
我遇到了类似的问题,发现行self.definesPresentationContext = YES;
是问题的根本原因(更多信息:UISearchController and definesPresentationContext)。
确保课程设置self.definesPresentationContext = YES;
也是搜索栏的父视图控制器。如果您使用您提供的代码初始化UISearchController
,然后将搜索栏分配给层次结构外的视图,则可能会导致问题。