3

I have seen several solutions to make UIStackView scroll with UIScrollView but they all rely Autolayout and IB.

Is there a way to do it programmatically?

I have seen this example: https://gist.github.com/twostraws/a02d4cc09fc7bc16859c

But it uses Visual Format Language and I am using the Layout Anchor API.

The layout anchor API:

view.leadingAnchor.constraintEqualToAnchor(otherview.topAnchor).active = true
MGY
  • 7,245
  • 5
  • 41
  • 74
Foobar
  • 7,458
  • 16
  • 81
  • 161
  • Bsically what you need to do is to add stack view to scrollview and make the content view of scroll view larger than the scroll view frame to make the scrol view scroll! Try giving the safe screen size of around 200x200, and don't worry about autolayout. – Teja Nandamuri Jun 17 '16 at 19:56
  • Could you provide a code example, so I can mark it as an answer and implement it? – Foobar Jun 17 '16 at 21:06
  • I have provided an example using layout anchors in my answer to this question http://stackoverflow.com/questions/31668970 – titaniumdecoy Sep 12 '16 at 17:38
  • Possible duplicate of [Is it possible for UIStackView to scroll?](http://stackoverflow.com/questions/31668970/is-it-possible-for-uistackview-to-scroll) – titaniumdecoy Sep 12 '16 at 17:38

2 Answers2

2

I was looking to do the same thing and stumbled upon this excellent post. To summarize, embed your UIStackView in your UIScrollView, and continue in the direction you were thinking by setting the anchor constraints of the UIStackView to match those of the UIScrollView:

stackView.leadingAnchor.constraintEqualToAnchor(scrollView.leadingAnchor).active = true
stackView.trailingAnchor.constraintEqualToAnchor(scrollView.trailingAnchor).active = true
stackView.bottomAnchor.constraintEqualToAnchor(scrollView.bottomAnchor).active = true
stackView.topAnchor.constraintEqualToAnchor(scrollView.topAnchor).active = true
stackView.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true
Dalmazio
  • 1,835
  • 2
  • 23
  • 40
  • Related question but not a duplicate: http://stackoverflow.com/questions/31668970/is-it-possible-for-uistackview-to-scroll/43408486#43408486 – Dalmazio Apr 14 '17 at 09:12
0

You can try ScrollableStackView : https://github.com/gurhub/ScrollableStackView

Sample Code (Swift)

import ScrollableStackView

var scrollable = ScrollableStackView(frame: view.frame)
view.addSubview(scrollable)

// add your views with 
let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
rectangle.backgroundColor = UIColor.blue
scrollable.stackView.addArrangedSubview(rectangle)
// ...

Sample Code (Objective-C)

@import ScrollableStackView

ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
scrollable.stackView.alignment = UIStackViewAlignmentCenter;
scrollable.stackView.axis = UILayoutConstraintAxisVertical;
[self.view addSubview:scrollable];

UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
[rectangle setBackgroundColor:[UIColor blueColor]];

// add your views with
[scrollable.stackView addArrangedSubview:rectangle]; 
// ...

Hope it helps.

MGY
  • 7,245
  • 5
  • 41
  • 74