UISearchBar not fitting in UINavigationBar on iPad - iOS 8

769 views Asked by At

I am trying to place a UISearchBar in a UINavigationBar's titleView.

I have two solutions, both are inconvenient.

First is :

[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:searchResultsController]];
[self.searchController setSearchResultsUpdater:searchResultsController];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[self.searchController setDelegate:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController.searchBar setDelegate:self];
[self.navigationItem setTitleView:self.searchController.searchBar];
[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setShowsCancelButton:YES];

When not searching, the result looks okay.

Not searching mode

But when search starts, the titleView extends too much on the right. Indeed, cancel button is visible, but just not on screen...

enter image description here

As in this answer I tried to wrap the UISearchBar in a another UIView before adding it to the navigationItem.

[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:searchResultsController]];
[self.searchController setSearchResultsUpdater:searchResultsController];
[self.searchController setHidesNavigationBarDuringPresentation:NO];
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
[self.searchController setDelegate:self];
[self.searchController setDimsBackgroundDuringPresentation:NO];
[self.searchController.searchBar setDelegate:self];

[self.searchController.searchBar setShowsCancelButton:NO];
[self.searchController.searchBar sizeToFit];
UIView *titleViewWrapper = [[UIView alloc] initWithFrame:self.searchController.searchBar.frame];
[titleViewWrapper addSubview:self.searchController.searchBar];
[self.navigationItem setTitleView:titleViewWrapper];

[self setDefinesPresentationContext:YES];
[self.searchController.searchBar setShowsCancelButton:YES];

And the result it the following:

Not searching:

enter image description here

Searching:

enter image description here

I am managing left/right bar button item showing/hiding myself in UISearchController's delegate methods.

This occurs in the Simulator, only on iPad.

Any ideas?

0

There are 0 answers