UIKit, StackView and Handling Rotation

Hi!

I'm building an app that has a scrollview which contains a StackView which then contains a button, an image, and a textview. I put everything in the scrollview so I can pinch/zoom the entire thing.

I'm struggling with constraints and rotating the device. If I start in portrait, everything lays out fine. But when I rotate the screen, my stackview does not expand to fill the whole screen. If I start in landscape and switch to portrait, the contents don't adjust properly either.

I've tried setting widthAnchors and that doesnt seem to help.

I've built the View programmatically and would like to handle everything programmatically.

I can post code snippets if needed.

Bobby

Answered by rdkapple in 752006022

Hi!

I solved the problem after even more googling and with a few suggestions via the InterWebs.

All the views should have translateAutoresizeMaskIntoConstraints = false

textView should have scrolling disabled. Its already in a scrollView.

Add a UIView so the view hierarchy is View -> ScrollView -> UIView -> StackView -> image,label,textview

Set scrollView constraints to edges of View Set UIView constraints to edges of scrollView Set UIView centerXAnchor sontraint to scrollView centerXAnchor to insure centering Set StackView contraints to edges of contentView

When thats all done, the app correctly handles device/screen rotation. No need to do anything on rotation.

Bobby

Here are some pictures of my test app.

Start in landscape, layout is what I expect.

After rotate to portrait, sizes don't seem to adjust.

Start in portrait, layout is what I expect.

Rotate to landscape, sizes don't adjust.

Accepted Answer

Hi!

I solved the problem after even more googling and with a few suggestions via the InterWebs.

All the views should have translateAutoresizeMaskIntoConstraints = false

textView should have scrolling disabled. Its already in a scrollView.

Add a UIView so the view hierarchy is View -> ScrollView -> UIView -> StackView -> image,label,textview

Set scrollView constraints to edges of View Set UIView constraints to edges of scrollView Set UIView centerXAnchor sontraint to scrollView centerXAnchor to insure centering Set StackView contraints to edges of contentView

When thats all done, the app correctly handles device/screen rotation. No need to do anything on rotation.

Bobby

UIKit, StackView and Handling Rotation
 
 
Q