我有一个 tabbarcontroller,它位于 uinavigationcontroller 内部,我试图在其中一个选项卡的 tableviewheader 中放置一个搜索栏。我能够在导航 Controller 上的任何屏幕上成功使用下面的代码,但当 View 位于选项卡 Controller 上时,搜索不会响应触摸。我可以在代码中成功将其设置为事件状态,但我也需要触摸才能工作。
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];
最佳答案
我遇到了类似的问题,并发现 self.definesPresentationContext = YES;
行是问题的根本原因(有关更多信息: UISearchController and definesPresentationContext )。
确保类设置 self.definesPresentationContext = YES;
也是搜索栏的父 View Controller 。如果您使用提供的代码初始化 UISearchController
,然后将搜索栏分配给层次结构外部的 View ,这可能会导致问题。
关于ios - UITabBarController 中的 UISearchController 不响应触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32079761/