如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?
我正在使用Xcode 6 Beta 3,iOS 8 SDK.使用Swift构建目标iOS 7.0.请通过以下屏幕截图逐步查看我的问题.
我在Storyboard中有一个UICollectionView.1原型UICollectionViewCell,中心包含1个标签(无自动调整规则).紫色背景是标记我在运行时由Cell生成的contentView.该视图最终将基于我的UICollectionViewLayoutDelegate正确调整大小,但不会在iOS 7上调整大小.请注意,我使用的是Xcode 6,问题只发生在iOS 7上.
当我在iOS 8上构建应用程序时.一切都很好.
注意:紫色是contentView,蓝色是我的带有圆角的UIButton.

但是,在iOS 7上,Cell中的所有子视图突然缩小到(0,0,50,50)的帧,并且永远不再符合我的自动调整规则.

我认为这是iOS 8 SDK或Swift或Xcode中的错误?
更新1:官方Xcode 6.0.1中仍存在此问题!最好的解决方法就像KoCMoHaBTa在下面通过设置单元格的cellForItem中的框架所建议的那样(你必须为你的单元子类化).事实证明,这是iOS 8 SDK和iOS 7之间的不兼容性(请查看Apple引用的ecotax的答案).
更新2: 将此代码粘贴到cellForItem的开头,事情应该没问题:
/** Xcode 6 on iOS 7 hot fix **/
cell.contentView.frame = cell.bounds;
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
/** End of Xcode 6 on iOS 7 hot fix **/
Run Code Online (Sandbox Code Playgroud) 我在UITableView上遇到了一个非常奇怪的问题.我有一些不同的单元格,我想在类似新闻源的表视图上显示.
我重用后我的细胞有问题.单元格包含两个图像,两个标签和一个带共享/喜欢/注释按钮的视图.我会尝试绘制它,但我不确定它会漂亮:
-------------------------------------------------
| | __ ____________________________ |
| | |_2| | | |
| | | 3 | |
| | | | |
| 1 | |____________________________| |
| | |
| |_______________________________________|
| | |
| | 4 |
|_______|_______________________________________|
| |
| 5 |
|_______________________________________________|
1 : objectImage
2 : subjectImage
3 : titleLabel
4 : commentLabel
5 : actionsView
Run Code Online (Sandbox Code Playgroud)
我的约束是:
"H:|-0-[objectImage(60)]-7-[subjectImage(30)]-7-[titleLabel]-7-|"
"H:|-0-[objectImage(60)]-0-[commentLabel]-0-|"
"H:|-0-[actionsView]-0-|"
"V:|-0-[objectImage(>=35)]-0-[actionsView(44)]-0-|"
"V:|-7-[subjectImage(30)]"
"V:|-7-[titleLabel(>=46)]-7-[commentLabel(>=35)]-0-[actionsView(44)]-0-|"
Run Code Online (Sandbox Code Playgroud)
每次绘制单元格时,我都会更改此约束:
"V:[commentLabel(%0.2f)]"
Run Code Online (Sandbox Code Playgroud)
细胞第一次完美展示.
但是,我遇到的问题是,在某些时候,经过一些重用(我不能每次都重现它),应用程序因为一个约束问题而崩溃.这是一个例外:
"<NSLayoutConstraint:0x205a75b0 V:[UILabel:0x1ef31430(105)]>",
"<NSLayoutConstraint:0x1ef34440 V:[UILabel:0x1ef31430]-(0)-[UIView:0x1ef2fe50]>",
"<NSLayoutConstraint:0x1ef34380 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将自动布局合并到我的UITableViewHeaderFooterView子类中.这个课程非常基础,只有两个标签.这是完整的子类:
@implementation MBTableDetailStyleFooterView
static void MBTableDetailStyleFooterViewCommonSetup(MBTableDetailStyleFooterView *_self) {
UILabel *rightLabel = [[UILabel alloc] init];
_self.rightLabel = rightLabel;
rightLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_self.contentView addSubview:rightLabel];
UILabel *leftLabel = [[UILabel alloc] init];
_self.leftLabel = leftLabel;
leftLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_self.contentView addSubview:leftLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(rightLabel, leftLabel);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[leftLabel]-(>=10)-[rightLabel]-10-|" options:0 metrics:nil views:views];
[_self.contentView addConstraints:horizontalConstraints];
// center views vertically in super view
NSLayoutConstraint *leftCenterYConstraint = [NSLayoutConstraint constraintWithItem:leftLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[_self.contentView addConstraint:leftCenterYConstraint];
NSLayoutConstraint *rightCenterYConstraint = [NSLayoutConstraint constraintWithItem:rightLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_self.contentView attribute:NSLayoutAttributeCenterY …Run Code Online (Sandbox Code Playgroud) 我正在以编程方式使用自动布局约束,并且我经常在我的应用程序中看到同样的错误,通常与看起来像这样的约束相关:
"<NSAutoresizingMaskLayoutConstraint:0x82da910 h=--& v=--& V:[UITableViewCellContentView:0x82d9fb0(99)]>"
Run Code Online (Sandbox Code Playgroud)
我在https://github.com/nicolasccit/AutoLayoutCellWarning上放了一些示例代码来重现
在这个例子中,我创建了一个包含2个UI元素的非常简单的视图:一个名为imageThumbnail的图像视图和一个名为labelName的标签,带有一些约束:
"H:|-padding-[_imageThumbnail(==imageWidth)]-[_labelName]";
"V:|-padding-[_imageThumbnail(==imageHeight)]-padding-|";
"V:|-padding-[_labelName]";
Run Code Online (Sandbox Code Playgroud)
在这两个元素上,我将AutoresizingMaskIntoConstraints设置为NO.
我得到以下例外:
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:0xa6e4f90 …Run Code Online (Sandbox Code Playgroud) 当我在iPhone上运行我的应用程序时,我收到以下错误.当我在模拟器中运行它时,我没有.如果我把它-12-|带走,那么单元格的高度就会崩溃到30像素.用户界面打破了.有人能帮帮我,告诉我为什么吗?
谢谢
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:0x170285ff0 V:|-(12)-[UIImageView:0x1741ec200] (Names: '|':UITableViewCellContentView:0x17419c7d0 )>",
"<NSLayoutConstraint:0x170286040 …Run Code Online (Sandbox Code Playgroud) uitableview ×5
autolayout ×4
ios ×4
ios8 ×2
objective-c ×2
cocoa-touch ×1
ios7 ×1
nsautolayout ×1
row-height ×1
xcode6 ×1