[英]UIStackView inside UIScrollview
我添加了UIScrollView,其中在情節提要中添加了UIStackView。 我正在嘗試在父UIStackView中動態添加具有兩個UILabel的UIStackView盡管我給出了子視圖的高度,但它占用了父UIStackView的所有空間,我如何解決此問題或任何其他方法來實現此目的?
我的代碼是
func createSummaryView()
{
let labelSummary:UILabel = UILabel(frame: CGRectZero)
labelSummary.text = "Summary"
labelSummary.heightAnchor.constraintEqualToConstant(30).active = true
let summaryparentView:UIStackView = UIStackView(frame: CGRectZero)
summaryparentView.axis = .Vertical
summaryparentView.alignment = .Leading
summaryparentView.spacing = 1
let summaryViewWidth = width!-50
summaryparentView.heightAnchor.constraintEqualToConstant(180).active = true
//summaryparentView.heightAnchor.constraintEqualToAnchor(summaryparentView.heightAnchor, multiplier: 2).active = true
summaryparentView.addArrangedSubview(labelSummary)
for _ in 1...5
{
let viewParent:UIStackView = UIStackView(frame: CGRectZero)
viewParent.axis = .Horizontal
viewParent.widthAnchor.constraintEqualToConstant(summaryViewWidth).active = true
viewParent.heightAnchor.constraintEqualToConstant(30).active = true
viewParent.distribution = .FillEqually
let labelTitle:UILabel = UILabel()
let labelValue:UILabel = UILabel()
print("summary view width \(summaryViewWidth)")
labelTitle.text = "BUILD"
labelValue.text = "2012"
viewParent.addArrangedSubview(labelTitle)
viewParent.addArrangedSubview(labelValue)
summaryparentView.addArrangedSubview(viewParent)
summaryparentView.translatesAutoresizingMaskIntoConstraints = false
}
stackViewVertical.layoutMarginsRelativeArrangement = true
stackViewVertical.addArrangedSubview(summaryparentView)
stackViewVertical.translatesAutoresizingMaskIntoConstraints = false
}
其中width為width = stackViewVertical.frame.size.width
也許這個庫可以幫助您: https : //github.com/gurhub/ScrollableStackView
它與Objective-C和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];
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.