什么时候应该将transformsAutoresizingMaskIntoConstraints设置为true? [英] When should translatesAutoresizingMaskIntoConstraints be set to 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.
View调试层次结构中的以下内容将给出警告"宽度和位置不明确".
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:
- 您用代码创建了一个基于
UIView
的对象(如果文件启用了自动布局,Storyboard/NIB会为您设置该对象), - 您要对此视图使用自动布局,而不是基于框架的布局,
- 该视图将使用自动布局添加到是的视图层次结构中.
- You Create a
UIView
-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled), - And you want to use auto layout for this view rather than frame-based layout,
- 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
返回标题视图后,将其添加到表格视图,并根据表格视图的当前宽度和从heightForHeaderInSection
返回的高度来设置其frame
.
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
如果您使用的是tableview.sectionHeaderHeight = UITableViewAutomaticDimension
这篇关于什么时候应该将transformsAutoresizingMaskIntoConstraints设置为true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!