UISearchController searchBar frame only resizes on first click
Asked Answered
M

2

3

I have just implemented a UISearchController in a regular UIViewController, and I have a kind of weird issue.

I have a UIView that is adapted exactly to the size I want my UISearchBar to take. On first launch of the view, I add my UISearchBar as a SubView to this UIView. But when I launch it, the UISearchBar doesn't take the size of its parent UIView -- it just takes the width of the whole screen.

enter image description here

As you can see, it overlaps with the button on the right.

But once I click on the search bar and cancel it, it resizes to the exact size I want.

enter image description here

What could be the issue here? I've tried adding AutoLayout Constraints from the SearchBar to its parent view but it doesn't seem to change anything. Doesn't [UIView addSubview:] handle this?

Mcadoo answered 7/4, 2017 at 12:36 Comment(0)
A
3

UISearchController's view behaviour is weird indeed, but there is a simple workaround.

  • Use an additional container view
  • put UISearchController's view inside
  • set UISearchController's view autoresizing mask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
  • set UISearchController's frame to container's view bounds

At this point UISearchController.view won't resize beyond container.bounds.

Abbate answered 7/4, 2017 at 12:56 Comment(6)
Thanks for your message ! I don't exactly understand -- this is what i'm doing right now, right? I'm already adding the SearchBar to my container!Mcadoo
If I understand correctly your container at the moment is the view of top most view controller (aka visibleVC.view) with uiseachcontroller.view added as it's subview. What I suggest is add additional container view (width ~80% of the screen and height equal to uiseachcontroller.view) as subview of visibleVC.view. Then add uisearchcontroller view to this newly created container view.Abbate
I already have a Container View that has the exact size of the UISearchBar in the second picture -- that's the UIView I add the UISearchController too. That's why I don't understand it -- it works after I click on it once, but on first launch it doesn't work.Mcadoo
Have you tried applying autoresizing masks flexibleWidth & flexibleHeight to the uisearchcontroller.view and setting it's frame to container view bounds ?Abbate
'searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; searchController.searchBar.frame = searchBarView.bounds;' Amazingly, it works perfectly. Thank you so much. Please post it as an answer and I will accept it!Mcadoo
For Additional ContainerView +1!! That's what I needed. Thank you!Hampstead
B
0

For me this didn't work, but I found a solution that I would like to share:

Instead of putting the searchBar in a containerView, put a navigationBar in the containerView, and put the searchBar in this navigationBar. For me, the problem still exists at this point.

But then I put a 1-pixel wide view as a navigationItem to the right of the navigationBar. From then it works all fine, the textfield of the searchBar didn't stretch anymore after the first selection.

It is more of a hack instead of a good solution to an annoying bug(?), but for me this hack is acceptable as I already needed some margins on both side of the searchBar. Here is some code:

//on init or viewDidLoad

navigationBar = UINavigationBar(frame: .zero)

let navigationItem = UINavigationItem()
navigationItem.titleView = searchController.searchBar

let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: Design.margin, height: 1))
leftView.backgroundColor = .clear
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftView)

let rightView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: Design.margin, height: 1))
rightView.backgroundColor = .clear
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightView)


navigationBar.items = [navigationItem]

containerView.addSubview(navigationBar)

// setup frame sizes in your layout

Britanybritches answered 27/9, 2018 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.