[英]UIScrollView is not scrolling till the end of the child 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的高度。 但是它仍然滾動很小的數量,並且不超過此數量。
我已經為兩個視圖添加了邊界約束
我該怎么辦?
您應該將堆棧視圖的四個邊緣限制為滾動視圖的四個邊緣。 自動布局基於滾動視圖及其后代之間的約束來設置滾動視圖的contentSize
。
@Saad Qureshi,您可以嘗試ScrollableStackView : https : //github.com/gurhub/ScrollableStackView
示例代碼(快速)
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];
// ...
讓我知道是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.