UIView 的 StackView 约束
StackView constraints with UIViews
我正在尝试在屏幕中间设置一个 stackview,左右填充 20。在里面,我想放置两个自定义 UIView,但我不明白该怎么做。我试着给 UIViews 各自的高度和宽度,但我什么也没得到。
我相信堆栈视图具有所有正确的约束。这是代码:
func setupTeamViews() {
view.addSubview(teamsStackView)
teamsStackView.distribution = .fill
teamsStackView.axis = .vertical
teamsStackView.spacing = 20
teamsStackView.alignment = .fill
NSLayoutConstraint.activate([
teamsStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
teamsStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
teamsStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
teamsStackView.heightAnchor.constraint(equalToConstant: 420)
])
let height = CGFloat((teamsStackView.frame.height / 2) - 20)
let width = CGFloat((teamsStackView.frame.width - 20))
firstTeamView = ATTeamView(width: width, height: height)
firstTeamView.changeColor(color: .lightBlue)
firstTeamView.setTeamName(name: "Tobias")
firstTeamView.setNewPoints(points: "0")
secondTeamView = ATTeamView(width: width, height: height)
secondTeamView.changeColor(color: .white)
secondTeamView.setTeamName(name: "Valen")
secondTeamView.setNewPoints(points: "0")
teamsStackView.addArrangedSubview(firstTeamView)
teamsStackView.addArrangedSubview(secondTeamView)
}
stackview 如何与 UIview 协同工作?据我了解,UIView 没有 intrinsicContentSize,但我不知道如何处理它。
您可以添加两个容器视图,将它们附加到您的视图控制器,然后使用 alpha 激活它们,而不是以编程方式进行。
这将使约束更容易,您仍然可以编写任何您想要的额外内容
myView1.alpha = 1 // activate
myView2.alpha = 0 // deactivate
我的代码无法正常工作,因为我忘记设置 teamsStackView.translatesAutoresizingMaskIntoConstraints = false
我正在尝试在屏幕中间设置一个 stackview,左右填充 20。在里面,我想放置两个自定义 UIView,但我不明白该怎么做。我试着给 UIViews 各自的高度和宽度,但我什么也没得到。
我相信堆栈视图具有所有正确的约束。这是代码:
func setupTeamViews() {
view.addSubview(teamsStackView)
teamsStackView.distribution = .fill
teamsStackView.axis = .vertical
teamsStackView.spacing = 20
teamsStackView.alignment = .fill
NSLayoutConstraint.activate([
teamsStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
teamsStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
teamsStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
teamsStackView.heightAnchor.constraint(equalToConstant: 420)
])
let height = CGFloat((teamsStackView.frame.height / 2) - 20)
let width = CGFloat((teamsStackView.frame.width - 20))
firstTeamView = ATTeamView(width: width, height: height)
firstTeamView.changeColor(color: .lightBlue)
firstTeamView.setTeamName(name: "Tobias")
firstTeamView.setNewPoints(points: "0")
secondTeamView = ATTeamView(width: width, height: height)
secondTeamView.changeColor(color: .white)
secondTeamView.setTeamName(name: "Valen")
secondTeamView.setNewPoints(points: "0")
teamsStackView.addArrangedSubview(firstTeamView)
teamsStackView.addArrangedSubview(secondTeamView)
}
stackview 如何与 UIview 协同工作?据我了解,UIView 没有 intrinsicContentSize,但我不知道如何处理它。
您可以添加两个容器视图,将它们附加到您的视图控制器,然后使用 alpha 激活它们,而不是以编程方式进行。
这将使约束更容易,您仍然可以编写任何您想要的额外内容
myView1.alpha = 1 // activate
myView2.alpha = 0 // deactivate
我的代码无法正常工作,因为我忘记设置 teamsStackView.translatesAutoresizingMaskIntoConstraints = false