UIKit 以编程方式:滚动视图和堆栈视图
我有一个堆栈视图,嵌套在堆栈视图中,嵌套在滚动视图中。
card 是一个 StackView,vStackContainer 是一个 StackView,两者都嵌套在滚动视图中。 由于某种原因,当我尝试复制卡片并将其显示在屏幕上时,我只得到一张而不是更多,如果我尝试使用普通的 UILabel 切换卡片,它工作正常。 这是为什么?
卡代码:
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = ColorsConstants.secondary
v.layer.cornerRadius = 5
v.axis = .horizontal
v.spacing = 15
v.distribution = .fillEqually
v.layoutMargins = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
v.isLayoutMarginsRelativeArrangement = true
return v
}()
vStackContainer 代码:
let v = UIStackView()
v.axis = .vertical
v.translatesAutoresizingMaskIntoConstraints = false
v.spacing = 20
v.distribution = .equalSpacing
return v
}()
viewDidLoad:
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(vStackContainer)
vStackContainer.addArrangedSubview(mainLabel)
for _ in 0...<15 {
card.addArrangedSubview(cardImage)
card.addArrangedSubview(cardTitle)
card.addArrangedSubview(cardDifficulty)
vStackContainer.addArrangedSubview(card)
}
同样,将 card
StackView 与常规 UILabel 交换,可以在屏幕上显示 15 个标签,但上面的代码仅在屏幕上呈现一张卡。
I have a stackview, nested in a stackview, nested in a scrollView.
card is a StackView, and vStackContainer is a StackView, both nested inside a scrollview.
For some reason when I try to duplicate the card and present it on screen, I only get one and not more, if I try to switch the card with a normal UILabel, it works fine.
Why is that?
Card code:
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = ColorsConstants.secondary
v.layer.cornerRadius = 5
v.axis = .horizontal
v.spacing = 15
v.distribution = .fillEqually
v.layoutMargins = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
v.isLayoutMarginsRelativeArrangement = true
return v
}()
vStackContainer code:
let v = UIStackView()
v.axis = .vertical
v.translatesAutoresizingMaskIntoConstraints = false
v.spacing = 20
v.distribution = .equalSpacing
return v
}()
viewDidLoad:
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(vStackContainer)
vStackContainer.addArrangedSubview(mainLabel)
for _ in 0...<15 {
card.addArrangedSubview(cardImage)
card.addArrangedSubview(cardTitle)
card.addArrangedSubview(cardDifficulty)
vStackContainer.addArrangedSubview(card)
}
Again, swapping the card
StackView with a regular UILabel gives me 15 labels on screen, but the code above only renders one card on screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论