UIScrollViews subviews not resizing themselves on Portrait to Landscape Orientation Change
Asked Answered
B

3

1

In my app, I have a hierarchy like Main view -> UIScrollView - > UISearchBar. The search bar covers the whole width of screen. But when it switches to landscape the scrollview adjust itself according to landscape and cover whole screen but its subviews like UISearchBar width remain same as in portrait. Plus I'm using Autolayout and I've correctly set their constraints (Leading and Trailing, width (greater than equal to 320) etc) in storyboard.

Also I know there are several answers for this in SO but almost everyone is saying set width again in willAutororate. But I have around 15 subviews in UIScrollview which I think is not possible to set programatically (as increase LOC) plus I think Autolayout must have some solution to it.

Blissful answered 9/1, 2014 at 10:16 Comment(1)
I would explain it, but i think this guy does it better than I could ever. codehappily.wordpress.com/2013/11/14/… on a side note though, if you want to set the constraints of the subviews you can run a for-in loop on the subviews.Fourwheeler
J
2

try this : this problem will solve by constraints only. I created a project and by using correct constraints its working fine for me ,

here I am going to show you how to add correct constraint one by one :

  1. add scrollview and add constraints for scrollview only like in the screenshots :

enter image description here

2. now add search bar and add constraints for search bar too, 
here below you can find out complete constraints for scrollview and search together:

enter image description here

Jochebed answered 9/1, 2014 at 18:38 Comment(1)
It's working. I forgot to enter horizontal center align. ThanksBlissful
O
12

Scrollviews don't play very well with constraints. Here's an approach using constraints and Interface Builder that worked for me for having a vertically scrolling scrollview that doesn't require any manual or content size finagling and works with rotation:

  • Scrollview in a view controller positioned with constraints
  • A container (UIView) that is a subview of the scrollview positioned with constraints pinning each edge to the superview
  • Add all your additional subviews to the container view instead. Use constraints. Exceed the height of the scrollview to get a vertically scrolling scrollview.

There is one trick however to get rotation resizing to work automagically:

  • You have to add an additional constraint to the container view to match its width to the UIScrollview's width. It's strange that you have to do this, since pinning each side to the scrollview should do the trick, but alas it won't work with rotation if you don't add the additional constraint.

Here's what I mean:

UIScrollView with constraints in Interface Builder

Outdare answered 22/1, 2015 at 3:30 Comment(1)
The same approach can be used for horizontally scrolling scrollviews. Just add that additional constraint for the height instead of the width and then add your subviews to exceed the width to scroll horizontally.Outdare
J
2

try this : this problem will solve by constraints only. I created a project and by using correct constraints its working fine for me ,

here I am going to show you how to add correct constraint one by one :

  1. add scrollview and add constraints for scrollview only like in the screenshots :

enter image description here

2. now add search bar and add constraints for search bar too, 
here below you can find out complete constraints for scrollview and search together:

enter image description here

Jochebed answered 9/1, 2014 at 18:38 Comment(1)
It's working. I forgot to enter horizontal center align. ThanksBlissful
V
0

You could use UIViewAutoresizing mask (autoresizingMask property) of your subviews.

Vivanvivarium answered 9/1, 2014 at 11:13 Comment(1)
Did you try setting autoresizingMask = UIViewAutoresizingFlexibleWidth to, i.e. your search bar?Vivanvivarium

© 2022 - 2024 — McMap. All rights reserved.