猿问

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

我正在以编程方式使用自动布局约束来布局自定义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 )>",

)


Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>

而且的确有在该列表中,我不想约束之一:


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

而且我无法将的translatesAutoresizingMaskIntoConstraints属性设置contentView为NO =>会弄乱整个单元格。


44是默认的单元格高度,但是我在表格视图委托中定义了我的自定义高度,那么为什么单元格contentView具有此约束?是什么原因造成的?


在iOS6中这没有发生,并且在iOS6和iOS7上一切都看起来很好。


我的代码很大,因此我不会在这里发布它,但是如果您需要它,可以随时请求一个pastebin。


要指定我的操作方式,有关单元格初始化:


我创建了所有标签,按钮等

我将其translatesAutoresizingMaskIntoConstraints财产设置为“否”

我将它们添加为contentView单元格的子视图

我在 contentView

我也很想了解为什么仅在iOS7上会发生这种情况。


PIPIONE
浏览 625回答 3
3回答

largeQ

我也遇到了这个问题。似乎在layoutSubviews调用contentView的框架之前,它不会得到更新, 但是单元格的框架会更早更新,而在{0, 0, 320, 44}评估约束时将contentView的框架设置为。详细查看contentView之后,似乎不再设置autoresizingMask。在约束视图之前设置autoresizingMask可以解决此问题:- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{&nbsp; &nbsp; self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];&nbsp; &nbsp; if (self)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;&nbsp; &nbsp; &nbsp; &nbsp; [self loadViews];&nbsp; &nbsp; &nbsp; &nbsp; [self constrainViews];&nbsp; &nbsp; }&nbsp; &nbsp; return self;}

12345678_0001

显然,使用iOS 8 SDK的iOS 7上的UITableViewCell和UICollectionViewCell出了问题。像这样重用单元格时,可以更新单元格的contentView:对于静态UITableViewController:#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{&nbsp; &nbsp; UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];&nbsp; &nbsp; if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; cell.contentView.frame = cell.bounds;&nbsp; &nbsp; &nbsp; &nbsp; cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;&nbsp; &nbsp; }&nbsp; &nbsp; //your code goes here&nbsp; &nbsp; return cell;}#endif#endif由于静态表视图控制器易碎,如果实现某些数据源或deletegate方法,则很容易损坏-有检查将确保仅在iOS 7上编译和运行此代码它与标准动态UITableViewController类似:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{&nbsp; &nbsp; static NSString *cellID = @"CellID";&nbsp; &nbsp; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];&nbsp; &nbsp; if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; cell.contentView.frame = cell.bounds;&nbsp; &nbsp; &nbsp; &nbsp; cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;&nbsp; &nbsp; }&nbsp; &nbsp; //your code goes here&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return cell;}对于这种情况,我们不需要额外的编译检查,因为需要实现此方法。这两种情况以及UICollectionViewCell的想法都是相同的,就像在此线程中所评论的:仅在iOS 7上运行时,才会在Storyboard原型单元(Xcode 6,iOS 8 SDK)中自动调整UICollectionViewCell contentView的框架大小
随时随地看视频慕课网APP

相关分类

iOS
我要回答