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

它在iOS6上运行得很好,在iOS7上也运行得很好

但当我在iOS7上运行这款应用时,我在控制台上看到了这样的消息:

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

列表中有一个我不想要的限制:

"<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是默认的单元格高度,但是我在表视图委托中定义了我的自定义高度,那么为什么单元格contentView有这个约束呢?是什么导致了这一切?

在iOS6中,它不会发生,在iOS6和iOS7上,一切看起来都很好.

我的代码很大,所以我不会把它贴在这里,但如果你需要的话,可以随意索要一个粘贴箱.

要指定我是如何做到这一点的,请参见单元初始化:

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

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

推荐答案

我也有这个问题..似乎直到调用layoutSubviews之后,contentView的框架才会更新,但是单元格的框架更新得更早,在判断约束时将contentView的框架设置为{0, 0, 320, 44}.

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

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

- (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]

Ios相关问答推荐

Flutter中抛出后期应用错误

无法在fastlane和github操作中从react-native 应用程序构建ios

AlGolia InstantSearch Swift Package Manager引发编译错误

SwiftUI.从自定义视图修改器访问自定义视图子视图

SWIFT-仅解码感兴趣的元素

使用标头UICollectionViewFlowLayout时的简单SwiftUI视图错误

iOS 16.4,SwiftUI 底部工作表有时会忽略presentationDetents

swiftui 图像放大时无法正确拖动

创建方案时运行 pod install 时出错

应用程序被杀死时如何在 flutter ios 应用程序中播放音频?

SwiftUI 动画:为什么容器视图在移动?我很难理解这种行为

如何在 Mac OS Ventura 中使用 Xcode 13

VStack 显示错误实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'

SwiftUI withAnimation 在视图出现之前不会启动

Got Unrecognized selector -replacementObjectForKeyedArchiver: 在 Swift 中实现 NSCoding 时崩溃

如何在 iOS 的 xib 中设置十六进制 colored颜色 代码

@IBDesignable 崩溃代理

如何在 UILabel 中找到文本子字符串的 CGRect?

闭包不能隐式捕获变异的 self 参数

使用 xib 创建可重用的 UIView(并从情节提要中加载)