ios - 不同 UIStackView 的子视图之间的等高约束
问题描述
我有一个约束场景,当通过界面构建器构建时它可以完美运行,但我无法通过代码实现它。画面布局如下:
UIStackView
具有多个垂直内联的水平UIStackView
。这些vertical 中的每一个UIStackView
都有多个UIView
'。我想要实现的是:inline UIView with equal height
通过界面生成器创建此类约束时,它可以完美运行:
但是当通过代码创建这个约束时,我遇到了以下错误:
'Unable to activate constraint with anchors <NSLayoutDimension:0x600002b9a6c0 "UIView:0x7f9e6f075c30.height"> and <NSLayoutDimension:0x600002b9a700 "UIView:0x7f9e6c423d50.height"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
我尝试使用以下方式创建这些约束:
1.
let heightConstraint = NSLayoutConstraint(item: view1,
attribute: .height,
relatedBy: .equal,
toItem: view2,
attribute: .height,
multiplier: 1,
constant: 0)
NSLayoutConstraint.activate([heightConstraint])
2.
let heightConstraint = NSLayoutConstraint(item: view1,
attribute: .height,
relatedBy: .equal,
toItem: view2,
attribute: .height,
multiplier: 1,
constant: 0)
horizontalStackView.addConstraint(heightConstraint)
存放horizontalStackView
所有垂直堆栈视图的水平堆栈视图在哪里。
我无法弄清楚这些约束是在哪里添加的,以便通过界面生成器通过代码创建它们。
解决方案
你应该关注“因为他们没有共同的祖先”部分。该问题可能与您的视图未添加到同一个容器视图有关。
我不知道你是如何创建你的视图的,但在某个时候你需要做一些类似的事情self.addSubview(yourView)
,这将解决你的问题。
推荐阅读
- r - 向 Shiny 发送 POST 请求
- arrays - C中的循环数组和消除,如何返回最后一个“活”元素索引?
- bash - 如何在 Bash 脚本中循环编辑多个文件?
- c# - 无法使用 C# HttpClient 访问公共 API
- c# - XML 反序列化 - 相同的元素名称不同的 xsi:type。无需验证
- python - 使用 Python Selenium 单击后无法访问同一网页中的元素
- amazon-web-services - 我应该在 S3 中 Dockerize Angular App 还是 Host Static
- google-sheets - 如果单元格以数字列表项(“1.”、“2.”等)开头的条件格式公式
- c++ - 我对碰撞方程有疑问
- r - Flexdashboard 和 DT:数据表未按预期工作