首页 > 解决方案 > 自动布局中是否存在隐藏约束

问题描述

我有一个完美的复杂布局。现在我想用一个可调整大小的视图覆盖现有布局,例如 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)

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

标签: cocoaautolayout

解决方案


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


推荐阅读