带有 UIViews 的 StackView 约束

托比亚斯·鲁阿诺

我正在尝试在屏幕中间设置一个堆栈视图,向左和向右填充 20。在里面,我想放置两个自定义 UIViews,但我不知道该怎么做。我试着给 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)
}

堆栈视图如何与 UIview 一起使用?据我了解, UIViews 没有internalContentSize,但我不知道如何处理。

托比亚斯·鲁阿诺

我的代码不起作用,因为我忘记设置 teamsStackView.translatesAutoresizingMaskIntoConstraints = false

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章