2

I am trying to get the new iOS-11 searchBar approach to work, but don't manage. The searchBar shows up, but is shown below the left bar button, instead of next to it:

enter image description here

It is the same in landscape mode. The code that I am using is:

self.searchResults = [[SearchResultsTable alloc] init];
self.searchResults.delegate = self;
searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResults];
searchController.searchResultsUpdater = self.searchResults;
searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                         target:self
                         action:@selector(swapToListView:)];
if ([self.navigationItem respondsToSelector:@selector(setSearchController:)]) {
    // only available from IOS-11 onwards
    self.navigationItem.title = nil;
    self.navigationItem.titleView = nil;
    [self.navigationItem setSearchController:searchController];

} else {
    // Fallback on earlier versions
    self.navigationItem.titleView = searchController.searchBar;
}

The old code works fine on pre-iOS-11 systems, but has other problems on iOS-11, so I prefer to use the new approach. Anybody knows what I am doing wrong here?

fishinear
  • 6,101
  • 3
  • 36
  • 84

1 Answers1

0

If you want to put search bar next to left navigation item then make your search bar navigationItem's titleView regardless of iOS version, and don't use UINavigationItem's searchController property.

Almas Sapargali
  • 451
  • 4
  • 9
  • Yes, that is the old approach that worked fine in iOS-10 and earlier. But there are major sizing issues with that approach in iOS-11. I got it to work in the end in iOS-11, but with a lot of manual sizing. – fishinear Jan 23 '18 at 12:33