问题描述
我已阅读文档.但我仍然不确定何时不需要将其设置为 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 调试层次结构中的以下内容将给出警告width and position are ambiguous".
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
返回标题视图后,它会添加到 table view 中,并且它的 frame
根据 table view 的当前宽度和高度设置从 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!