How to make autoLayout for UISearchController while rotating the device?

I use UISearchController to integrate with my table view. So that my search does not scroll with the table, I already made a view (name SearchBarView) under the navigation bar, and my table is under this view. The entire element in my view is configured using the automatic layout: enter image description here

In my code. I am doing a UISearchController as a subclass of SearchBarView and using the sideToFit function to automatically set the screen width as follows:

self.resultSeachController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.searchBar.sizeToFit() // add searchbar in the searchbarView self.searchBarView.addSubview(controller.searchBar) return controller })() 

My SearchBar works fine now, except for one thing: when I rotate the simulator, the searchBar does not automatically fit the width of the screen. It only fits the screen when I touch SearchBar to search for something. enter image description hereenter image description hereenter image description hereenter image description here

I do not know how to fix it. I tried this code:

  override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { self.resultSeachController.searchBar.sizeToFit() } 

But it still does not work. How can i fix this?

+5
source share
1 answer

Do this during the animation along with the transition:

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { [_searchController.searchBar sizeToFit]; } completion:nil]; } 
+3
source

All Articles