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

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

        UITableViewCell中iOS7上的自动布局约束问题

        Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)

          <tfoot id='wUtT4'></tfoot>
        • <small id='wUtT4'></small><noframes id='wUtT4'>

              • <bdo id='wUtT4'></bdo><ul id='wUtT4'></ul>
                  <tbody id='wUtT4'></tbody>

                  <legend id='wUtT4'><style id='wUtT4'><dir id='wUtT4'><q id='wUtT4'></q></dir></style></legend>
                  <i id='wUtT4'><tr id='wUtT4'><dt id='wUtT4'><q id='wUtT4'><span id='wUtT4'><b id='wUtT4'><form id='wUtT4'><ins id='wUtT4'></ins><ul id='wUtT4'></ul><sub id='wUtT4'></sub></form><legend id='wUtT4'></legend><bdo id='wUtT4'><pre id='wUtT4'><center id='wUtT4'></center></pre></bdo></b><th id='wUtT4'></th></span></q></dt></tr></i><div id='wUtT4'><tfoot id='wUtT4'></tfoot><dl id='wUtT4'><fieldset id='wUtT4'></fieldset></dl></div>
                • 本文介绍了UITableViewCell中iOS7上的自动布局约束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在以编程方式使用自动布局约束来布局我的自定义 UITableView 单元格,并且我在 tableView:heightForRowAtIndexPath:

                  中正确定义了单元格大小

                  它在 iOS6 上运行良好,在 iOS7 上也看起来正常

                  但是当我在 iOS7 上运行应用程序时,我在控制台中看到的消息是这样的:

                  中断 objc_exception_throw 以在调试器中捕获它.<UIKit/UIView.h> 中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助.2013-10-02 09:56:44.847 Vente-Exclusive [76306:a0b] 无法同时满足约束条件.以下列表中的至少一个约束可能是您不想要的.试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它.(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)("<NSLayoutConstraint:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50] (名称:'|':UITableViewCellContentView:0xd93e850)>","<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>","<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>","<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>","<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-|(名称:'|':UITableViewCellContentView:0xd93e850)>","<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>")将尝试通过打破约束来恢复<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>

                  而且确实在那个列表中有一个我不想要的约束:

                  "<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"

                  我无法将 contentViewtranslatesAutoresizingMaskIntoConstraints 属性设置为 NO => 它会弄乱整个单元格.

                  44 是默认单元格高度,但我在表格视图委托中定义了我的自定义高度,那么为什么单元格 contentView 有这个约束?什么可能导致这种情况?

                  在 iOS6 中没有发生这种情况,在 iOS6 和 iOS7 上一切看起来都很好.

                  我的代码比较大,这里就不贴了

                  在单元初始化时指定我的操作方式:

                  • 我创建了我所有的标签、按钮等
                  • 我将他们的 translatesAutoresizingMaskIntoConstraints 属性设置为 NO
                  • 我将它们添加为单元格 contentView 的子视图
                  • 我在 contentView
                  • 上添加了约束

                  我也很想了解为什么这种情况只发生在 iOS7 上.

                  解决方案

                  我也遇到了这个问题.. 似乎 contentView 的框架在调用 layoutSubviews 之前不会更新,但是框架单元格的 较早更新,在评估约束时将 contentView 的框架设置为 {0, 0, 320, 44}.

                  更详细地查看 contentView 后,似乎不再设置 autoresizingMasks.

                  在约束视图之前设置 autoresizingMask 可以解决此问题:

                  - (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];如果(自己){self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;[自加载视图];[自我约束视图];}回归自我;}

                  Swift5、ios14:

                  contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]

                  I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath:

                  It's working just fine on iOS6 and it does look fine in iOS7 as well

                  BUT when I run the app on iOS7, here's the kind of message I see in the console:

                  Break on objc_exception_throw to catch this in the debugger.
                  The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
                  2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] Unable to simultaneously satisfy constraints.
                      Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
                  (
                          "<NSLayoutConstraint:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50]   (Names: '|':UITableViewCellContentView:0xd93e850 )>",
                          "<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",
                          "<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>",
                          "<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>",
                          "<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-|   (Names: '|':UITableViewCellContentView:0xd93e850 )>",
                          "<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
                  )
                  
                  Will attempt to recover by breaking constraint 
                  <NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>
                  

                  And indeed there's one of the constraint in that list I don't want :

                  "<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
                  

                  and I cannot set the translatesAutoresizingMaskIntoConstraints property of the contentView to NO => it would mess up the entire cell.

                  44 is the default cell height but I defined my custom heights in the table view delegate so why does the cell contentView has this constraint? What could cause this?

                  In iOS6 it's not happening and everything looks just fine on both iOS6 and iOS7.

                  My code is quite big so I won't post it here but feel free to ask for a pastebin if you need it.

                  To specify how I'm doing it, on cell intialization:

                  • I create all my labels, buttons, etc
                  • I set their translatesAutoresizingMaskIntoConstraints property to NO
                  • I add them as subviews of the contentView of the cell
                  • I add the constraints on the contentView

                  I'm also deeply interested in understanding why this happens only on iOS7.

                  解决方案

                  I had this problem as well.. It appears that the contentView's frame doesn't get updated until layoutSubviews is called however the frame of the cell is updated earlier leaving the contentView's frame set to {0, 0, 320, 44} at the time when the constraints are evaluated.

                  After looking at the contentView in more detail, It appears that autoresizingMasks are no longer being set.

                  Setting the autoresizingMask before you constrain your views can resolve this issue:

                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
                  {
                      self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                      if (self)
                      {
                          self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
                          [self loadViews];
                          [self constrainViews];
                      }
                      return self;
                  }
                  

                  Swift5, ios14:

                  contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
                  

                  这篇关于UITableViewCell中iOS7上的自动布局约束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)

                  <small id='Ppv9c'></small><noframes id='Ppv9c'>

                • <tfoot id='Ppv9c'></tfoot>

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