1

In Android, one used ScrollView as the root in an XML file, then include many other different kinds of views inside it, that allowed the scroll behavior.

How does Swift 2.1 and Xcode 7.1.1 do this, since the storyboard is not long enough to insert all the different kinds of views I want to put in, like I did in the XML code for Android?

I am thinking iOS with an Android brain :(

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Fred J.
  • 5,759
  • 10
  • 57
  • 106

2 Answers2

2

There is a scroll view for iOS also in the interface builder object library:

enter image description here

Start to finish here is how to make it work in storyboard.

1: go to you view controller and click on Attribute Inspector.

2: change Size to Freeform instead of Inferred.

3: Go to the main view on that storyboard, not your scrollview but rather the top level view.

4: Click Size Inspector and set this view to your desired size. I changed my height to 1000.

Now you will see that you storyboard has your view setup so you can see the entire height of your scroll for easy design.

5: Drop on a scrollview and stretch it so it takes up the whole view. You should now have a scrollview with size of 320,1000 sitting on a view in your view controller.

Now we need to make it scroll and need to make it show content correctly.

6: Click on your scrollview and click on Identity Inspector.

7: Add a User Defined runtime attribute with KeyPath of contentSize then type of SIZE and put in your content size. For me it is (320, 1000).

Since we want to see our whole scroll view on the storyboard we stretched it and it has a frame of 320,1000 but in order for this to work in our app we need to change the frame down to what the visible scrollview will be.

8: Add a runtime attribute with KeyPath frame with Type RECT and 0,0,320,416.

Now when we run our app we will have a visible scrollview has a frame of 0,0,320, 416 and can scroll down to 1000. We are able to layout our subviews and images and whatnot in Storyboard just the way we want them to appear. Then our runtime attributes make sure to display it properly. All of this without 1 line of code.

Is this what you were thinking of?

If you want the scroll view to change size I would recommend trying this:

enter image description here

You want to do is drop the scroll view onto the view controller and and add constraints.

I have never used a scroll view before, so this might not work.

Community
  • 1
  • 1
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • I need the scroll view to automatically adjust its hight to the content. does that mean set height to zero in step 4? – Fred J. Dec 07 '15 at 18:07
  • setting the frame to (0,0,320,416), does that relate to a certain device screen size? if so, how will it adapt to different screen sizes? – Fred J. Dec 07 '15 at 18:44
  • See http://stackoverflow.com/a/34140935/218152: do not hard code anything, let AutoLayout do all the hard work. – SwiftArchitect Dec 07 '15 at 19:18
2

0 lines of code

Storyboard is long enough:

All you need to do is to create a freeform view, put all your content is that freeform view using top-to-bottom Autolayout constraints, and use that view as the content of your UIScrollView.

Freeform

Tutorial

  1. View Controller > Show the Attributes inspector > Size > Freeform
  2. View Controller > Show the Size Inspector > Simulated Size > Freeform > width & height
  3. Add a UIScrollView
  4. Add 4 AutoLayout constraints, with top/left relative to superview, and bottom/right of superview relative to scrollview
    • Scroll.Top = Superview.Top Margin
    • Scroll.Leading = Superview.Leading
    • Bottom Layout Guide.Top = Scroll.Bottom
    • Scroll.Trailing = Superview.Trailing
  5. Add a UIView as a subview to UIScrollView
  6. Repeat 4 AutoLayout constraints, same rule: anchored top, superview relative to subview width/height
    • Content.Top = Scroll.Top
    • Content.Leading = Scroll.Leading
    • Scroll.Bottom = Content.Bottom
    • Content.Trailing = Scroll.Trailing
  7. Add all subviews to that UIView. Ensure you can trace an AutoLayout chain of constraints all the way from top to bottom (and left to right) with again the same rule as 4. and 6. anchored top & superview.width and superview.height relative to content.
  8. Vertical height of your content view is handled by 7. For the width, and in this example, I decided to go full width. Notice how the Content view's width is relative to the root view width:
    • Content.width = View.width

Continuous edge-to-edge constraints

Methodology

  • Create a hierarchy of views using the following structure:

    1. root UIView (the one owned by the UIViewController
    2. UIScrollView scroller (must stretch with relationship to the root)
    3. content UIView (which will dictate the scroller scroll bars & area)
    4. everything else goes into the content view
  • Understand what dictates the size of the content

    1. either hard sizes
    2. either relationship to contained views (continuous constraints)
    3. either relativity to superviews
  • Understand the the edge-to-edge AutoLayout constraint continuity rule

    1. You should be able to follow a continuous set of constraints top-to-bottom or left-to-right for the directions that will define the size of en enclosing view
    2. You do not need such continuity for merely locating elements ; in fact, continuity where you do not need it may create conflicts
  • to make the enclosure size relative to the enclosed views (what you are trying to achieve vertically in your example):

    1. attach the fist top view to a rigid location
    2. attach each view underneath to the object above it
    3. attach the bottom of the enclosure to the bottom of the last object

► Find this solution on GitHub and additional details on Swift Recipes.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • I checked your project, after giving the mainView and the scrollView clearColour, and the contentView yellow. I am having hard time getting the contentView to cover the whole screen in different phone sizes. Is there a way to get this behaviour automatically? – Fred J. Dec 09 '15 at 06:25
  • You need to tell `contentView` how wide it must be, say *as wide as the view controller's root view*. I've updated the project. Also, if you want to give the impression of seamless content, both the `UIScrollView` and its content must have the same background color. – SwiftArchitect Dec 09 '15 at 17:40
  • I wish you could place a label in each of the 4 corners so that an examination of the code output in different iPhones can show if the display perform the same by placing each label in its respective corner on all devices. I am interested in portrait only but it would be nice if landscape is also considered. (without hardcoding the size of the width), I understand the height needs to be decided in advance. – Fred J. Dec 09 '15 at 23:34
  • I am not sure how to follow step 4. Add 4 AutoLayout constraints, with top/left relative to superview, and bottom/right of superview relative to scrollview. What do you mean "bottom/right of superview relative to scrollview" – Fred J. Dec 10 '15 at 00:18
  • Will put the responses to your comments in the answer. – SwiftArchitect Dec 10 '15 at 14:43