我已经在navigatiom栏中实现了一个带有搜索栏的UISearchController,我想在加载视图时使搜索栏处于活动状态.当我说活动时,我的意思是键盘出现,用户可以在不点击搜索栏的情况下输入他/她的搜索.
我使用以下代码初始化了UISearchController:
- (void)viewDidLoad
{
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[self.searchController setSearchResultsUpdater:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.navigationItem setTitleView:self.searchController.searchBar];
[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setDelegate:self];
[self.searchController.searchBar setShowsCancelButton:YES];
[self.searchController.searchBar setPlaceholder:@"City or Airfield"];
[self.searchController.searchBar sizeToFit];
}
Run Code Online (Sandbox Code Playgroud)
我试图让我的搜索控制器处于活动状态,通话[self.searchController.searchBar becomeFirstResponder]
并直接呼叫,searchBarSearchButtonClicked
但没有任何作用.