【问题标题】:Are there hidden constraints in auto-layout自动布局中是否存在隐藏约束
【发布时间】:2021-07-03 21:04:29
【问题描述】:

我有一个完美的复杂布局。现在我想用一个可调整大小的视图覆盖现有布局,例如 40 像素边距,相对于窗口。当我在内容视图中添加一个空的自定义视图时,约束系统变得混乱并说它不能满足约束,结果非常随机。

我将视图添加为:

    contentView.addSubview(customView)
    let dict = ["CustomView" : customView]
    let s1 = "|-(40)-[CustomView]-(40)-|"
    let s2 = "V:|-(40)-[CustomView]-(40)-|"
    let con1 = NSLayoutConstraint.constraints(withVisualFormat: s1, options: [],
                                                    metrics: nil, views: dict as [String : Any])
    let con2 = NSLayoutConstraint.constraints(withVisualFormat: s2, options: [],
                                                      metrics: nil, views: dict as [String : Any])
    viewLayout.append(contentsOf: con1)
    viewLayout.append(contentsOf: con2)
    NSLayoutConstraint.activate(viewLayout)

据我所知,这个视图不应该对底层内容视图的大小或形状添加额外的约束,而应该简单地调整到适当的大小。看起来一定是对视图的一些隐式约束,阻止它简单地调整大小,或者我错过了一些关于约束的基本内容。

【问题讨论】:

  • viewLayout 在上述代码之前是一个空数组。

标签: cocoa autolayout


【解决方案1】:

我得到了这个工作。我需要取消选中自定义视图的“将掩码转换为约束”复选框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多