Reputation: 470
I am trying to add a UISearchBar to my UINavigationItem but the scope bar is showing behind the search bar. Any ideas to fix that problem?
iOS: 11.2 xCode: 9.2
my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mySearchBar = [[UISearchBar alloc] init];
self.mySearchBar.delegate = self;
self.mySearchBar.scopeButtonTitles = @[@"item 1", @"item 2", @"item 3"];
self.navigationItem.titleView = self.mySearchBar;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[self.mySearchBar setShowsScopeBar: TRUE];
[self.mySearchBar setShowsCancelButton:TRUE animated:TRUE];
return TRUE;
}
the result:
Upvotes: 0
Views: 1105
Reputation: 1962
The problem is that UINavigationBar
provides a very specific and familiar iOS style that Apple wants to keep the same across apps. Default navigation bars don't expand to fit their contents.
When you set set the titleView
of navigation item, you are expected to lay out the contents in that view based on the size of the navigation bar, not the other way around.
There are several possible solutions:
UINavigationBar
(not recommended).UISearchBar
underneath the navigation bar as a regular subview.UISearchController
The first option should definitely not be your first solution because it requires you to solve many thorny issues. Use as a last resort.
Option 2 requires the following code changes. Replace self.navigationItem.titleView = self.mySearchBar
with:
[self.view addSubview:self.mySearchBar];
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[self.mySearchBar.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[self.mySearchBar.rightAnchor constraintEqualToAnchor:guide.rightAnchor].active = YES;
[self.mySearchBar.leftAnchor constraintEqualToAnchor:guide.leftAnchor].active = YES;
And you are also missing code to resize the UISearchBar
after showing the scope bar. The view does not resize itself. So, in your searchBarShouldBeginEditing:
method, add this line just before return: [self.mySearchBar sizeToFit];
The third solution may be easier for you depending on your use case. That is, use UISearchController
, which includes it's own UISearchBar
anchored at the top of the screen. It would look just like solution #2 above as shown in the image below:
Here is a great tutorial on using UISearchController
if you are interested.
Upvotes: 1