SearchBar in UISearchController Not Sizing

I have two issues here.

  1. The initial size of my searchbar in my UISearchController appears to be too wide when initially presented.
  2. The searchbar is not auto resizing if the orientation changes.

Notice the right side how the search bar appears to extend beyond the edge...

Then when I turn sideways I get the following (the UIView I'm using as a container I've painted purple. So I know it's resizing properly.. Just no the searchbar.)

Here's how I'm adding it to my view...

	self.searchResultsController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SearchAddressTableView"];

	self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];

	self.searchController.searchResultsUpdater = self;
	self.searchController.searchBar.placeholder = @"Search Address or Place";
	[self.searchController.searchBar sizeToFit];
	[self.searchControllerContainerView addSubview:self.searchController.searchBar];
	[self.searchController.searchBar setFrame:self.searchControllerContainerView.bounds];
	[self.searchController.view setFrame:self.searchControllerContainerView.bounds];
	[self.searchControllerContainerView bringSubviewToFront:self.searchController.searchBar];
	[self.searchController.searchBar setNeedsUpdateConstraints];
	[self.searchController.view setNeedsUpdateConstraints];
	[self.searchController.searchBar sizeToFit];
	self.searchController.delegate = self;
   self.searchController.obscuresBackgroundDuringPresentation = YES;
	self.searchController.searchBar.delegate = self;

	self.definesPresentationContext = YES;

	[super updateViewConstraints];

As you can see I've tried a number of different things with setting the bounds on not only the searchbar but also the controller's view as well.

I also added this to see if it would help but unfortunately no change...

- (void)viewWillLayoutSubviews
{
	[self.searchController.searchBar sizeToFit];
}

Any thoughts? I'm guessing it must have something to do with constraints. I just don't know how best to do that?

Your on the right track in viewWillLayoutSubviews (assuming you don't want to instead just use auto layout) but -sizeToFit: has relatively little information to go on – so its going to pick a fairly arbitrary size. Instead you should call -sizeThatFits: to get a size that is appropriate to your container, then set the frame on the search bar.

SearchBar in UISearchController Not Sizing
 
 
Q