我在一个垂直的堆栈视图中有两个视图(下半部分和上半部分)。它们的约束依赖于stackview。我在botHalfView中有一个水平stackview(containerStackView3)。它的引导、尾随和高度约束依赖于botHalfView。在调试窗口中,我还可以在控制台上看到bothalfview.frame.height=0的高度,我将其添加为horizontalstackview的约束。因此bothalf视图中的水平stackview没有显示任何维度。我应该将水平堆栈视图的高度指定给哪个约束?或者其他解决办法?

       let containerStackView3 = UIStackView()
        containerStackView3.translatesAutoresizingMaskIntoConstraints = false
        containerStackView3.axis = .vertical
        containerStackView3.distribution = .fillEqually
        containerStackView3.spacing = 1
 //adding views
        containerStackView1.addArrangedSubview(botHalfView2)
        botHalfView2.addSubview(containerStackView3)

//constraints bothalfview
        botHalfView2.bottomAnchor.constraint(equalTo:
        containerStackView1.bottomAnchor, constant: 0).isActive = true
        botHalfView2.leadingAnchor.constraint(equalTo:
        containerStackView1.leadingAnchor, constant: 0).isActive = true
        botHalfView2.trailingAnchor.constraint(equalTo:
        containerStackView1.trailingAnchor, constant: 0).isActive = true

// Constraints of the stack view inside the bothalfview
        containerStackView3.leadingAnchor.constraint(equalTo:
        botHalfView2.leadingAnchor, constant: 0).isActive = true
        containerStackView3.trailingAnchor.constraint(equalTo:
        botHalfView2.trailingAnchor, constant: 0).isActive = true
        containerStackView3.bottomAnchor.constraint(equalTo:
        botHalfView2.bottomAnchor, constant: 0).isActive = true

  containerStackView3.heightAnchor.constraint(equalToConstant:botHalfView2.frame.height).isActive = true

最佳答案

如果你需要和机器人一半一样高的容器,那么就这样做
//bothalfview中堆栈视图的约束

    containerStackView3.leadingAnchor.constraint(equalTo:
    botHalfView2.leadingAnchor, constant: 0).isActive = true
    containerStackView3.trailingAnchor.constraint(equalTo:
    botHalfView2.trailingAnchor, constant: 0).isActive = true
    containerStackView3.bottomAnchor.constraint(equalTo:
    botHalfView2.bottomAnchor, constant: 0).isActive = true
    containerStackView3.topAnchor(equalTo:
    botHalfView2.topAnchor, constant: 0).isActive = true

关于swift - 尝试获取2个 View 之一的frame.height,该 View 均等地填充到stackview内部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58457478/

10-10 20:52