How to make a ScrollView with AutoLayout in Xcode5

2019-01-22 14:28发布

In xcode 5 using storyboards how would one make a fully operational vertical scrolling scrollview, with AutoLayout ON?
Considering the subviews have hierarchy:

  1.UIView  
    2.UIScrollView
      3.UIView (lets call this UIDetailView to make things easier)

Please be specific from code to constraints to wether any of the views HAS to be smaller etc.

5条回答
forever°为你锁心
2楼-- · 2019-01-22 14:41

UIScrollView with Autolayout within Storyboards Just Works

I've seen a number of people recommending the 'Container View' approach, AKA brute force, to solving the problem that they don't understand. It is non-optimal since you now have lost a big advantage of the scrollview by making it think the content is the entire scrollview rather than the subviews immediately attached to the scrollview.

Here is how I did it in the example that follows

--UIScrollView
  |-> UITextView
  |-> UILabel
  |-> UIOtherStuff

When placing a UIScrollView into a UIView in a Storyboard just pin the edges to the 4 sides of the UIScrollView to the UIView. Now add your content to the UIScrollView making sure that you provide a minimum of two constraints for each dimension. The great thing about Autolayout is that it figures out how big the contentSize of the scrollview, or UILabels for that matter, needs to be based upon the size of the content inside it. AKA intrinsicContentSize. So if you are given a warning 'Ambiguous content size for scrollView' you know that you have not given the content enough constraints. For example, you might have given Top, Bottom, Left, Right spacing distance between views but the subview you're constraining needs a height too since an infinite vertical plane like this UIScrollView could assume your view was from zero to infinitely high.

To put it another way the Apple guide on Autolayout by Example makes a simple 3 point plan for success:

  1. Create the scroll view.
  2. Place the UI element inside it.
  3. Create constraints that fully define the width and height of the scroll view content.

That top TextView with 'Min melding til' is also growing as you type more lines into it and the whole ScrollView grows to contain it. While I override the UITextView class to return a modified height constraint, the ScrollView itself works correctly without coding.

One last thing, lots of posts related to Autolayout try the magical fix-all incantation translatesAutoresizingMaskIntoConstraints = NO. This is only necessary if the view is created programmatically.

Example of UIScrollView with Autolayout

查看更多
男人必须洒脱
3楼-- · 2019-01-22 14:48

This blog post details how to use a UIScrollView with Autolayout ON, using a pure autolayout approach. Note though that all constraints in the blog post are defined through the Storyboard.

The approach in the post assumes the following hierarchy:

1. View (main view of my UIViewController)
  2. Scroll View (UIScrollView)
     3. Container View (UIView)
       4. Content View (e.g. UIImageView)

I guess the Container View will be your UIDetailView, and the Content View will be any UIView inside your UIDetailView.

https://happyteamlabs.com/blog/ios-how-to-use-uiscrollview-with-auto-layout-pure-auto-layout/

查看更多
Evening l夕情丶
4楼-- · 2019-01-22 14:55

The documentation clearly states how to do this: A UIScrollView in auto-layout will always resize itself to fit the content (UIDetailView).

So you have to set up your views like this:

  • UIView: Position with constraints.
  • UIScrollView: Bind to UIView with constraints.
  • UIDetailView: Set size (intrinsic content size), max out compression-resistance, set top-, bottom-, leading- and trailing constraints to UIScrollView to 0 manually.
查看更多
三岁会撩人
5楼-- · 2019-01-22 15:02

I had a similar problem and i found relative simple solution similar to DJ S's from within Interface Builder using pure Autolayout without any code.

For proof-of-concept at first remove any constraint in View Controller to if see this works.

This is sample layout:

  • View (main view of my UIViewController)

    • Scroll View (UIScrollView)

      • Container View (UIView)

        • Content View (e.g. UIImageView)

A. Scroll View width/height should be smaller that Container View width/height

B. Container View should have some determinated width/height (may be explicit width/height )

C. Do Control-drag Container View to Scroll View and add only:

  1. Leading Space to Container
  2. Trailing Space to Container

D. Check out those two constraints and set "constant" value for both to 0

E. Run app and

查看更多
相关推荐>>
6楼-- · 2019-01-22 15:03

Because of the new iPhone 6 and 6+ screen sizes, I had to make a few tweaks to DJ S's solution.

The goal

Position a UITextView inside a UIScrollView, and also have 15 pt spaces from the left/right screen edges.

Views

1. Main View (main view of my UIViewController)
  2. Scroll View (UIScrollView)
     3. Container View (UIView)
       4. Text View (UITextView)

Solution

For the spaces, I added 15 pt horizontal trailing/leading spaces from UIScrollView->Main View. To make the UITextView's width relative to the screen width, I added an Equal Widths constraint from UITextView -> Main View and set the value to -30 (2 * the 15 pt horizontal space). Now, the UITextView's width will dynamically adjust for any screen size.

The UIScrollView should have Scrolling Enabled. The UITextView should not.

查看更多
登录 后发表回答