10
votes

I've implemented a UISearchController with its search bar in a navigatiom bar and I would like to make the search bar active when the view is loaded. When I say active, I mean that the keyboard appears and the user can type his/her search without tap the search bar.

I initialised the UISearchController with the following code:

- (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];
}

I've tried to make my search controller active, call [self.searchController.searchBar becomeFirstResponder] and directly call searchBarSearchButtonClicked but nothing works.

3
is searchController code is in viewDidLoadDipu Rajak
yes it is (I've edited the post)Nonouf
Just to make sure if with your current code when you tap the search bar everything works fine ...right? And where did you put '[self.searchController.searchBar becomeFirstResponder]' in viewDidLoad?SanitLee
Yes everything works fine. I first tried in viewDidLoad then in viewDidAppear with both the same result, nothing.Nonouf
So you already put '[self.searchController.searchBar becomeFirstResponder]' after initialising the search bar, correct?SanitLee

3 Answers

14
votes

Try calling

[self.searchController setActive:YES]

before

[self.searchController.searchBar becomeFirstResponder]

If the above is not working, try something like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self initializeSearchController];
    ....
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.searchController setActive:YES];
    [self.searchController.searchBar becomeFirstResponder];
}

- (void)initializeSearchController {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];

    [self.tableView setTableHeaderView:self.searchController.searchBar];
    self.definesPresentationContext = YES;
}
8
votes

To activate the search bar:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.searchController setActive:YES];
}

"becomeFistResponder" can be called to make the keyboard appear only when UISearchController is loaded .

- (void)didPresentSearchController:(UISearchController *)searchController
{
    [searchController.searchBar becomeFirstResponder];
}
1
votes

Besides doing what the other users suggested, I also did the following, and it worked:

searchController.definesPresentationContext = YES;