I'm using a UISearchController integrate with my tableview. For making my searchBar not scrolling with the table, I already made a View (name SearchBarView) under the navigation bar and my table is under this view. All element on my view is setting up using auto layout:
In my code. I make a UISearchController like a subview of SearchBarView and using sideToFit function for auto fit the width of the screen like this:
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 is working perfect now except one: when I rotate the simulator, the searchBar is not automatic fit the width of screen. It only fit the screen when I touch the SearchBar for searching something.
I don't know how to fix. I tried this code :
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.resultSeachController.searchBar.sizeToFit()
}
But It's still not working. How can I fix that?
Do it while animating along with the transition:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[_searchController.searchBar sizeToFit];
} completion:nil];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With