0

I am not able to add UISearch Controller on a navigation bar iOS 9.. There are many on the internet but none of them works..

The code is..

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.delegate = self;
    searchController.searchBar.delegate = self;
    searchController.hidesNavigationBarDuringPresentation = NO;
    searchController.dimsBackgroundDuringPresentation = YES;

    self.navigationItem.titleView = searchController.searchBar;

    self.definesPresentationContext = YES;

}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{}
Aryan Kashyap
  • 121
  • 1
  • 12
  • 2
    Possible duplicate of [Displaying search bar in navigation bar in iOS 8](http://stackoverflow.com/questions/30226835/displaying-search-bar-in-navigation-bar-in-ios-8) – stefos May 25 '16 at 16:40

1 Answers1

2

You should use. On your code above, your are set nil for it. So it can not init.

[searchController = [UISearchController alloc] initWithSearchResultController:self]

Here code for iOS 8/9:

UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self];
// Use the current view controller to update the search results.
searchController.searchResultsUpdater = self;
// Install the search bar as the table header.
self.navigationItem.titleView = searchController.searchBar;
// It is usually good to set the presentation context.
self.definesPresentationContext = YES;

Hopefully, it help you solve your issue. Keep your passion! Good luck.

Cuong Nguyen
  • 980
  • 2
  • 9
  • 29