I'm trying to show a simple settings screen in my app and I'm using Autolayout. Since my app supports landscape, and the settings controls don't quite fit vertically when the phone is landscape, I added a UIScrollview.
It seems that the combination of UIScrollview and Autolayout is a common question here and elsewhere. I've read a few articles on it, and from what it can tell, it seems that a good way to go for what I'm trying to do is to put my various controls within a view, like a "contentView" with constraints aligned in that view. Then have that contentView be the sole subview of the UIScrollView. I have the contentView with constraints tied to the edges of the UIScrollView.
UINavigationView -> UIScrollview -> contentView (UIView) -> Control Subviews
It works fine in portrait, even if I add content in my contentView that requires scrolling to see, but when I go to landscape, it doesn't let me scroll down far enough. It's like the content size gets reset to the frame of the visible area (or the frame of the navigation controller superview) when I rotate.
I'm wondering if I need to check for rotation and then set the content size again at that time? If so, is there a way to get that size dynamically from the view (contentView)?
Thanks for any help you can offer! Jim
There are two components to having auto layout properly handle scroll views:
Constraints for scroll view's subviews: The
contentSize
of the scroll view will be dictated by the constraints of the scroll view's subviews. Thus, you need to have a bottom constraint for the last control in the scroll view (i.e. second switch) to its superview (i.e. the scroll view). It looks like you do have this. These constraints for subviews of the scroll view will automatically adjust the scroll view'scontentSize
as needed.By the way, the
constant
for this second switch's bottom constraint will generally default to some largish value corresponding to how it was laid out in portrait. You may want to select and edit this bottom constraint for this last control and change it so that it's the "standard" value.Constraints for the scroll view itself: You need to make sure you have the bottom constraint for the scroll view, itself, to its superview (and make sure you do not have a height constraint on the scroll view). This will adjust the
frame
of the scroll view upon screen rotation. I wonder if this might be missing in your project.You can confirm this by running your app in the debugger, hitting "pause" button:
and then at the
(lldb)
prompt, typepo [[UIWindow keyWindow] recursiveDescription]
, and you should see something like:Check the
frame
of theUIScrollView
(e.g. the above is for landscape on iPhone), and make sure it corresponds to the size of screen. If you haven't defined the constraints between the scroll view and its superview, thisframe
may be incorrect.Maybe others will have a better solution for this, but the fastest without changing too much on your current build would be to do exactly what you said: check for rotation and then set the content size again. It would be done like this in your Settings View Controller implementation file:
... assuming you have added in that same file outlets to your
UIScrollView
and yourUIView
namedscrollView
andcontentView
respectively.