gpt4 book ai didi

ios - UIScrollview 里面的 UIStackView

转载 作者:行者123 更新时间:2023-11-30 13:12:32 25 4
gpt4 key购买 nike

我添加了 UIScrollView,其中在 Storyboard中添加了 UIStackView。我正在尝试在父 UIStackView 中动态添加具有两个 UILabels 的 UIStackView 虽然我给出了 subview 的高度,但它占用了父 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 = stackViewVertical.frame.size.width

最佳答案

也许这个库可以帮助你:https://github.com/gurhub/ScrollableStackView

它与 Objective-C 和 Swift 兼容。

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

关于ios - UIScrollview 里面的 UIStackView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38570147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com