2

I've read everything I could find about stacks with scroll views but nothing helps me.

I want to have vertical scroll because I have some labels in stack view and sometimes there are all labels sometimes there are not. According to this, I want to make scroll when needed. Here is a visual representation of my problem.

enter image description here

How could I achieve this? Do I have to code something or do some adjustments on storyboard?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Kira
  • 1,575
  • 4
  • 26
  • 48
  • You can do that only with storyboard and constraints, the contentSize of the scrollView is the intrinsic content size of the UIStackView, I can share the solution using storyboard if needed. hope that helps – HamzaGhazouani Mar 16 '16 at 10:10
  • Well it works with solution provided below. If I will experiment with it I will post something here so you'll know I need help. Thanks in advance. – Kira Mar 16 '16 at 12:38

1 Answers1

1

You can use contentSize property of UIScrollView for this purpose.

  scrollview.contentSize = CGSizeMake(contentWidth, contentHeight)

This property sets content size of the scrollview.

Lets take example, consider you have scrollview whose height is 480 and width is 320

If you want horizontal scrolling then set contentWidth greater then 320 and if you want vertical scrolling set contentHeight greater then 480

For your problem it should be like this

  yourscrollview.contentSize = CGSizeMake(your scrollview Width, Y of Last label + height of Last Label)

And don't forget to set

      scrBudgetList.scrollEnabled = true;
Indrajeet
  • 5,490
  • 2
  • 28
  • 43
  • I never want a horizontal scroll. So I have Scroll view in place, I set constraints but I'm lost in CGSizeMake function. I have something like scrollView.contentSize = CGSizeMake(scrollView.frame.width, newsLabel.frame.height) – Kira Mar 15 '16 at 13:43
  • I did it. I just had to set stack width same as scroll view. Thanks – Kira Mar 15 '16 at 13:53