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: 
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()
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. 



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?
source share