• <small id='z9NaP'></small><noframes id='z9NaP'>

    <tfoot id='z9NaP'></tfoot>
  • <i id='z9NaP'><tr id='z9NaP'><dt id='z9NaP'><q id='z9NaP'><span id='z9NaP'><b id='z9NaP'><form id='z9NaP'><ins id='z9NaP'></ins><ul id='z9NaP'></ul><sub id='z9NaP'></sub></form><legend id='z9NaP'></legend><bdo id='z9NaP'><pre id='z9NaP'><center id='z9NaP'></center></pre></bdo></b><th id='z9NaP'></th></span></q></dt></tr></i><div id='z9NaP'><tfoot id='z9NaP'></tfoot><dl id='z9NaP'><fieldset id='z9NaP'></fieldset></dl></div>

    <legend id='z9NaP'><style id='z9NaP'><dir id='z9NaP'><q id='z9NaP'></q></dir></style></legend>

        <bdo id='z9NaP'></bdo><ul id='z9NaP'></ul>

        何时应将 translatesAutoresizingMaskIntoConstraints 设置为 true?

        时间:2023-09-11

          <tfoot id='FJaV7'></tfoot>
          • <bdo id='FJaV7'></bdo><ul id='FJaV7'></ul>
                  <tbody id='FJaV7'></tbody>
                • <small id='FJaV7'></small><noframes id='FJaV7'>

                  <legend id='FJaV7'><style id='FJaV7'><dir id='FJaV7'><q id='FJaV7'></q></dir></style></legend>
                  <i id='FJaV7'><tr id='FJaV7'><dt id='FJaV7'><q id='FJaV7'><span id='FJaV7'><b id='FJaV7'><form id='FJaV7'><ins id='FJaV7'></ins><ul id='FJaV7'></ul><sub id='FJaV7'></sub></form><legend id='FJaV7'></legend><bdo id='FJaV7'><pre id='FJaV7'><center id='FJaV7'></center></pre></bdo></b><th id='FJaV7'></th></span></q></dt></tr></i><div id='FJaV7'><tfoot id='FJaV7'></tfoot><dl id='FJaV7'><fieldset id='FJaV7'></fieldset></dl></div>
                  本文介绍了何时应将 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.

                  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:

                  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 返回标题视图后,它会添加到 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='u6xce'></tfoot>
                      <bdo id='u6xce'></bdo><ul id='u6xce'></ul>
                          <tbody id='u6xce'></tbody>
                      1. <legend id='u6xce'><style id='u6xce'><dir id='u6xce'><q id='u6xce'></q></dir></style></legend>
                        <i id='u6xce'><tr id='u6xce'><dt id='u6xce'><q id='u6xce'><span id='u6xce'><b id='u6xce'><form id='u6xce'><ins id='u6xce'></ins><ul id='u6xce'></ul><sub id='u6xce'></sub></form><legend id='u6xce'></legend><bdo id='u6xce'><pre id='u6xce'><center id='u6xce'></center></pre></bdo></b><th id='u6xce'></th></span></q></dt></tr></i><div id='u6xce'><tfoot id='u6xce'></tfoot><dl id='u6xce'><fieldset id='u6xce'></fieldset></dl></div>

                      2. <small id='u6xce'></small><noframes id='u6xce'>