swift - 试图获取 2 个 View 之一的 frame.height,它在 stackview 中均匀填充

标签 swift autolayout uistackview

我在垂直堆栈 View 中有 2 个 View (bothalf 和 tophalf)。它们的约束取决于该堆栈 View 。我在 botHalfView 中有一个水平堆栈 View (containerStackView3)。它的前导、尾随和高度约束取决于 botHalfView。在调试窗口中,我还可以在控制台上看到 bothalfview.frame.height=0 的高度,我将其添加为 horizo​​ntalstackview 的约束。因此,bothalfview 内的 horizo​​ntalstackview 不显示任何维度。我应该将 horizo​​ntalstackview 的高度分配给哪个约束?或者对此有任何其他解决方案吗?

       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

最佳答案

如果你需要容器​​高度和 bot half 一样,那么这样做 //bothalfview 中堆栈 View 的约束

    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,它在 stackview 中均匀填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457478/

相关文章:

ios - UITableView 行在 UITableViewHeaderSection 下滑动

ios - 键盘打开时移动菜单(快速)

ios - 如何准备 UIViewController 的 View 以添加到 UIStackView?

ios - 使用下一个上层对象的 anchor

ios - Xcode 7.3 中的自动布局 (swift 2)

ios - 检测 UIStackView 间距中的点击(项目之间)

ios - 如何为堆栈 View 中的 View 添加约束

ios - 点击 UITableViewCell 类中的 UIImageView 时如何打开新屏幕?

ios - 当子类化 UIView 并将其添加到界面构建器中时,不会调用 drawRect

ios - 自动布局的优点和缺点