Reputation: 1158
I've run into a problem that is causing a major headache.
In Interface Builder, I have a scene that includes just a tableview. The controller for this subclasses UITableViewController. I have another scene that has a view at the top with a height of 50, spanning the width of the view. Below that is a container view that ultimately holds a list or a map. The controller subclasses UIViewController. Here's what it looks like on the simulator...
Note that the list you see in the above image is a completely different table and view controller. The searchbar controller is used to search for locations by city or zip. The table view for the searchbar controller overlays the other list. When a location is selected from the search table, the region for the location is fed to the other controller, which uses a private web service to retrieve a list of points of interest.
I'm calling the following method to add the searchbar to the view mentioned above.
- (void)setupSearchView {
self.searchTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationSearchTable"];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchTableViewController];
self.searchController.searchResultsUpdater = self.searchTableViewController;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.tag = 9189;
self.searchController.searchBar.placeholder = @"Search by city or ZIP code";
[self.searchView addSubview:self.searchController.searchBar];
[[self.searchView viewWithTag:9189] becomeFirstResponder];
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.dimsBackgroundDuringPresentation = true;
self.definesPresentationContext = true;
}
When I click in the text field, with self.definesPresentationContext set to true, the search bar drops down the exact height of the navigation bar, leaving a large white space above it.
If self.definesPresentationContext is false, then the behavior and result are different. In this case, when I click in the search field, everything looks fine. But as soon as I begin to type, the navigation controller is covered in white. The search bar does not change position. If I use Reveal, I can see that the nav bar has been pushed way back in the stack. Here's what that looks like...
What am I missing?
Upvotes: 4
Views: 953
Reputation: 3706
Swift 5.x solution:
Setting edgesForExtendedLayout to none or empty will fix this issue.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.edgesForExtendedLayout = []
}
Upvotes: 1
Reputation: 11
I had the same problem. Add a searchbar and its frame with each change on the screen so that it does not go down. This code should help solve the problem:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.searchController.searchBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50)
self.view.addSubview(searchController.searchBar)
}
And use here in viewDidLoad
:
self.extendedLayoutIncludesOpaqueBars = true
Upvotes: 1
Reputation: 151
I had the same issue try using the following line of code in your viewDidLoad of the presenting viewController.
self.extendedLayoutIncludesOpaqueBars = true
Upvotes: 1