translatesAutoresizingMaskIntoConstraints 何时应设置为 true?

问题描述 投票:0回答:3

我已阅读文档。但我仍然不确定什么时候需要将其设置为

true
。在下面的代码中,如果我将其设置为
false
我根本看不到标题。如果我将其保留为
true
,那么一切都很好。

查看调试层次结构中的以下内容将给出警告“widthposition不明确”。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let header = UIView()
    header.translatesAutoresizingMaskIntoConstraints = false
    header.backgroundColor = .orange
    header.heightAnchor.constraint(equalToConstant: 10).isActive = true
        
    return header
}

我想每当我需要修改代码中的任何内容时,我都必须将

translatesAutoresizingMaskIntoConstraints
设置为
false

也许更正确的说法是,如果您需要删除其所有约束,然后将其设置为

false
,然后添加您喜欢的内容,在这种情况下,您需要为所有 4 条边添加约束。

但是,如果您只需要保留系统提供给您的内容,在这种情况下,将由 tableView 管理其位置和宽度,然后留给

true

是吗?

ios swift uitableview autolayout nslayoutconstraint
3个回答
87
投票

translatesAutoresizingMaskIntoConstraints
在以下情况下需要设置为 false:

  1. 您在代码中创建一个基于
    UIView
    的对象(如果文件启用了自动布局,Storyboard/NIB 将为您设置它),
  2. 并且您想对此视图使用自动布局而不是基于框架的布局,
  3. 并且视图将被添加到is使用自动布局的视图层次结构中。

在这种情况下,并非所有这些都是正确的。 具体来说,第 2 点。

viewForHeaderInSection
返回标题视图后,它会添加到表格视图中,并且其
frame
是根据表格视图的当前宽度和从
heightForHeaderInSection
返回的高度设置的。

您可以将子视图添加到根标题视图(代码中的

header
)并使用约束来相对于标题视图布局这些子视图。

您已经在评论中发现了标题视图本身无法使用自动布局的原因;在创建视图时,它还不是视图层次结构的一部分,因此您无法将其边缘限制为任何内容。

为了动态调整标题大小,您需要向

header
视图添加子视图,并在这些子视图和
header
之间添加约束。 然后,自动布局可以使用
header
的固有内容大小来确定标题视图大小。

由于您没有约束

header
的框架,因此请勿将
translatesAutoresizingMaskIntoConstraints
设置为
false
。 您需要确保对自动布局的子视图有足够的约束,以确定
header
的大小。

如果子视图的内在内容大小不够,您将需要从上到下的连续约束线,并且可能需要子视图的一些高度约束。

您添加到

header
do 的任何子视图都需要将
translatesAutoresizingMaskIntoConstraints
设置为
false

您还需要从 estimatedHeightForHeaderInSection 返回

something
- 越接近实际标题高度越好 - 如果您使用
tableview.sectionHeaderHeight = UITableViewAutomaticDimension


59
投票
  • 对于以编程方式创建的视图,默认值为 true,对于 Interface Builder 中的视图默认为 false

    如果该属性为(或设置为)True,则系统会根据视图的框架及其自动调整大小蒙版自动创建一组约束。如果您添加自己的约束,它们不可避免地会与自动生成的约束发生“冲突”。这会产生令人不满意的布局。因此,当以编程方式实例化视图时,请务必将其 translatesAutoresizingMaskIntoConstraints 属性设置为 NO

有了

2
投票

答案

,终于明白了。
在我的例子中,我有一个ImageView

并且只设置了2个约束,但错误显示我有更多约束并且LayoutConstraints是

无法同时满足约束
view.addSubview(imageView) let yConstraint = imageView.centerYAnchor.constraint(equalTo: layout.centerYAnchor) yConstraint.identifier = "yConstraint" let xConstraint = imageView.centerXAnchor.constraint(equalTo: layout.centerXAnchor) xConstraint.identifier = "xConstraint"

错误信息:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x600000089360 h=--& v=--& UIImageView:0x7fc1782087b0.minY == 0   (active, names: '|':UIView:0x7fc176406220 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x600000089220 h=--& v=--& UIImageView:0x7fc1782087b0.height == 0   (active)>",
    "<NSLayoutConstraint:0x600000088c30 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide']-(34)-|   (active, names: '|':UIView:0x7fc176406220 )>",
    "<NSLayoutConstraint:0x600000088cd0 'UIView-topMargin-guide-constraint' V:|-(48)-[UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide']   (active, names: '|':UIView:0x7fc176406220 )>",
    "<NSLayoutConstraint:0x600000088d70 'yConstraint' UIImageView:0x7fc1782087b0.centerY == UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide'.centerY   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000088d70 'yConstraint' UIImageView:0x7fc1782087b0.centerY == UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide'.centerY   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

我设置的第二个约束(
"xConstraint"
)基本上有一个几乎相同的错误日志。正如您在错误日志中看到的,我“过度限制”了我的 UI。除了

'yConstraint'

 之外,我还有其他 4 个限制。我相信约束位于“元组”中,因此它们周围有括号。 XCode 试图通过提示“(注意:如果您看到您不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性的文档translatesAutoresizingMaskIntoConstraints)”来提供帮助,但我个人认为这不够有帮助。
什么是“自动调整大小蒙版”?
我想知道这意味着什么很重要,因为它正在转换为约束,因此得名

translatesAutoresizingMaskIntoConstraints
。 它是包含整数位掩码的 UIView 实例属性。它保留了一些位,您可以打开和关闭“灵活左边距”和“灵活高度”等功能(更多内容

此处

)。

当视图的边界发生变化时,该视图会根据每个子视图的自动调整大小掩码自动调整其子视图的大小。

来源

总结一下自动调整大小蒙版,它

保留了您想要的自动调整大小功能,例如灵活的高度和宽度。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.