什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true? [英] When should translatesAutoresizingMaskIntoConstraints be set to true?

查看:25
本文介绍了什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 false I won't see the header at all. If I leave it as true, then everything is fine.

查看调试层次结构中的以下内容将给出警告宽度位置不明确".

The following in View debug hierarchy will give a warning "width and position are ambiguous".

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.

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

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

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

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

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.

是吗?

推荐答案

translatesAutoresizingMaskIntoConstraints 需要设置为 false 时:

translatesAutoresizingMaskIntoConstraints needs to be set to false when:

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

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

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

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

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

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

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

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

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.

为了动态调整标题大小,您需要向 header 视图添加子视图,并在这些子视图和 header 之间添加约束.然后,自动布局可以使用 header 的内在内容大小来确定标题视图的大小.

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

由于您没有约束header 的框架,所以不要将translatesAutoresizingMaskIntoConstraints 设置为false.您需要确保对自动布局的子视图有足够的约束,以确定 header 的大小.

Since you are not constraining the frame of header, do not set translatesAutoresizingMaskIntoConstraints to false. You will need to ensure that you have sufficient constraints on your subviews for auto layout to determine the size of 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.

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

Any subviews you add to header do need translatesAutoresizingMaskIntoConstraints set to false

您还需要从 estimatedHeightForHeaderInSection 返回 something - 越接近您的实际标题高度越好 - 如果您使用的是 tableview.sectionHeaderHeight = UITableViewAutomaticDimension

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

这篇关于什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆