UIScrollView不滚动直到子UIStackView结束

时间:2016-12-08 04:14:19

标签: ios swift uiscrollview storyboard uistackview

我有一个UIScrollView,它有一个UIStackView作为它的孩子。

我想将它链接到内部堆栈视图的大小,以便它能够滚动到它的底部。

我尝试了许多其他方法,例如

scrollView.contentSize = CGSize(width: stackViewMain.frame.width, height: stackViewMain.frame.height)
scrollView.isScrollEnabled = true

scrollView.contentSize = CGSize(width: stackViewMain.frame.width, height: 1000)

我甚至尝试在故事板中硬编码scrollView的高度。但它仍然滚动很小的数量而不是更多。

我已经添加了两个视图的边缘约束

enter image description here enter image description here

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您应该将堆栈视图的四个边缘约束到滚动视图的四个边缘。 Autolayout根据滚动视图及其后代之间的约束设置滚动视图的contentSize

更多信息:Technical Note TN2154: UIScrollView And Autolayout

答案 1 :(得分:0)

@Saad Qureshi,您可以尝试 ScrollableStackView https://github.com/gurhub/ScrollableStackView

示例代码(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)
// ...

示例代码(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]; 
// ...

如果有帮助,请告诉我。