ios 什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47800210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:38:45  来源:igfitidea点击:

When should translatesAutoresizingMaskIntoConstraints be set to true?

iosswiftuitableviewautolayoutnslayoutconstraint

提问by Honey

I've read the documentation. But I'm still not sure when I need to not set it to false. In the code below if I set it to falseI won't see the header at all. If I leave it as true, then everything is fine.

我已阅读文档。但我仍然不确定何时不需要将其设置为false. 在下面的代码中,如果我将其设置为false我根本看不到标题。如果我将其保留为true,那么一切都很好。

The following in View debug hierarchy will give a warning "widthand positionare ambiguous".

以下 View 调试层次结构将给出警告“宽度位置不明确”。

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
}

I thought whenever I need to modify anything in the code I would have to set translatesAutoresizingMaskIntoConstraintsto false.

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

Perhaps it's more correct to say if you need to remove all its constraints then set it to falseand then add what you like, and in that case you would need to add constraints for all 4 sides.

也许更正确的说法是如果您需要删除其所有约束,然后将其设置为false然后添加您喜欢的内容,在这种情况下,您需要为所有 4 个边添加约束。

However, if you need to just keep what the system provides to you, in this case that would be the tableView managing its position and width then leave to true.

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

Is that right?

那正确吗?

回答by Paulw11

translatesAutoresizingMaskIntoConstraintsneeds to be set to false when:

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

  1. You Create a UIView-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled),
  2. And you want to use auto layout for this view rather than frame-based layout,
  3. And the view will be added to a view hierarchy that isusing auto layout.
  1. UIView在代码中创建一个基于对象(如果文件启用了自动布局,故事板/NIB 将为您设置它),
  2. 并且您想为此视图使用自动布局而不是基于框架的布局,
  3. 和视图将被添加到一个视图层次使用自动布局。

In this case not all of these are true. Specifically, point 2.

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

After you return the header view from viewForHeaderInSectionit is added to the table view and its frameis set based on the current width of the table view and the height you return from heightForHeaderInSection.

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

You can add subviews to the root header view (headerin your code) and use constraints to layout those subviews relative to the header view.

您可以将子视图添加到根标题视图(header在您的代码中)并使用约束来相对于标题视图布局这些子视图。

You have discovered the reason why you can't use autolayout for the header view itself in your comments; at the time you create the view it isn't yet part of the view hierarchy and so you cannot constrain its edges to anything.

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

In order to have dynamic header sizing, you will need to add subviews to your headerview and add constraints between those subviews and header. Then, auto layout can use the intrinsic content size of headerto determine the header view size.

为了具有动态标题大小,您需要将子视图添加到您的header视图中,并在这些子视图和header. 然后,自动布局可以使用 的内在内容大小header来确定标题视图大小。

Since you are not constraining the frame of header, do not set translatesAutoresizingMaskIntoConstraintsto false. You will need to ensure that you have sufficient constraints on your subviews for auto layout to determine the size of header.

由于您没有约束 的框架header,所以不要设置translatesAutoresizingMaskIntoConstraintsfalse。您需要确保对自动布局的子视图有足够的约束来确定header.

You will need a continuous line of constraints from top to bottom and potentially some height constraints for your subviews if the intrinsic content size of that subview is not sufficient.

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

Any subviews you add to headerdoneed translatesAutoresizingMaskIntoConstraintsset to false

您添加的任何子视图header需要translatesAutoresizingMaskIntoConstraints设置为false

You also need to return somethingfrom estimatedHeightForHeaderInSection- the closer to your actual header height the better - if you are using tableview.sectionHeaderHeight = UITableViewAutomaticDimension

您还需要返回的东西estimatedHeightForHeaderInSection-越接近实际的标题高度的更好-如果你正在使用tableview.sectionHeaderHeight = UITableViewAutomaticDimension

回答by PANKAJ VERMA

  • For programmatically created view default is trueand for views from Interface Builder default is false

    If the property is (or set to) True, the system automatically creates a set of constraints based on the view's frame and its autoresizing mask. And if you add your own constraints, they inevitably conflictwith the autogenerated constraints. This creates an unsatisfiable layout. So When programmatically instantiating views, be sure to set their translatesAutoresizingMaskIntoConstraintsproperty to NO.

  • 对于以编程方式创建的视图,默认值为true,对于来自 Interface Builder 的视图,默认值为false

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