我已经添加了UIScrollView,其中在故事板中添加了UIStackView。 我试图在父UIStackView中动态添加具有两个UILabel的UIStackView虽然我给出了子视图的高度,它占用了父UIStackView的所有空间我如何解决这个或任何其他方式来实现这个? / p>
我的代码是
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
答案 0 :(得分:0)
也许这个图书馆可以帮到你: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];
// ...