UITableViewCell 中 iOS7 上的自动布局约束问题 [英] Auto layout constraints issue on iOS7 in UITableViewCell

查看:29
本文介绍了UITableViewCell 中 iOS7 上的自动布局约束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它在 iOS6 上运行良好,看起来在 iOS7 中也很好

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

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

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)]>"

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

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

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

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?

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

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:

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

我也很想知道为什么这只会在 iOS7 上发生.

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

推荐答案

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

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.

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

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

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

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:

Swift5, ios14:

contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]

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

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