Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController: search bar initially too wide

I have a problem with the UISearchBar programmatically added inside a UIView (on one of the child views of a TabBarController):

When the screen is first loaded, the search bar is too wide to fit fully into the view. However, when you tap inside and then tap the Cancel button the search bar is resized so that it fits well into the UIView.

I uploaded a sample project on GitHub - the incorrect behaviour can be observed on the second tab, in the BlueViewController class. I've tried numerous things already, as you can probably see in the code and searched through the most popular StackOverflow answers, but none of them fixed this problem for me so far.

Here's the link to the project: https://github.com/asia-t/tabbartest

It would be great if someone could shed some light on what may be going on there.

enter image description here

like image 659
Joanna Derks Avatar asked Mar 22 '16 20:03

Joanna Derks


1 Answers

you could implement the following method to fix the searchbar's frame:

override func viewDidLayoutSubviews() {
    var searchBarFrame = searchController.searchBar.frame
    searchBarFrame.size.width = vanillaSearchBar.frame.size.width
    searchController.searchBar.frame = searchBarFrame
}
like image 101
André Slotta Avatar answered Nov 12 '22 17:11

André Slotta