简体   繁体   中英

UIScrollView in Storyboard not working with iOS 8 Size Classes and Autolayout

So I'm trying to create a UIScrollView only in storyboard that allows me to add scrolling labels for more than the height of the VC. Here's what I did:

  1. Created UIScrollView that took up the size of the any width any height VC

  2. Made constraints 0 for spacing to nearest neighbor on top, bottom, left, and right

  3. Created a view that is a subView of the UIScrollView with the same width as the any width any height VC but height of 1500 (because I only want it to scroll vertically).

  4. Set constraints to nearest neighbor as 0 for ONLY left, top, and right and set the height constraint as 1500.

  5. I put a label at the top of the subView and at the bottom

When I run the app on an iPhone 6, does not scroll vertically as I want it to. Any ideas why this is not working? Thanks in advance.

To obtain the scroll you have to pin the sub view (the "content view") to the top, left, bottom and right of the scrollview. In addition you have to give it (to the "content view") an explicit (=not related to the scroll view) height and width because these are used by the scrollview to calculate its content size. In your case set the width equal to the VC main view width and the height to 1500. When width or height are bigger than the scrollview size, it will scroll.

Scroll view constraints (pinned to main view)

滚动视图约束

Content view (pinned to scrollview + height 1500, width=mainview width) + label constraints (as an example: 20 20 from content view top left)

内容视图+标签约束

For an easier visualisation, I created a video on how to do that.

Video on how to create a vertical-only scrollview in iOS

Have you set the contentsize bigger than the screen itself? In your case, just bigger in height.

As Apple Documentation says: "You must set the contentSize property to the size of the scrollable content. This specifies the size of the scrollable area."

The only solution is add constraints to the right and bottom of the bottom subview of scroll view's child view. Also check if all the views from top to bottom have got proper constraints along with height. For the scroll view ' s wrapper view add equal width and equal height constraint to its superview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM