【发布时间】:2014-02-25 16:11:13
【问题描述】:
我在使用 xib 文件创建的自定义 TableViewCell 中使用自动布局,它看起来像这样:
一切都按预期工作并且显示正确,但我在向下滚动 TableView 时收到很多警告消息:
无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) (
"<NSLayoutConstraint:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>",
"<NSLayoutConstraint:0xc3c1dc0 V:|-(10)-[UIImageView:0xc396fc0] (Names: '|':UITableViewCellContentView:0xc396f20 )>",
"<NSLayoutConstraint:0xc3c1fc0 V:[UIImageView:0xc3b7700]-(0)-| (Names: '|':UITableViewCellContentView:0xc396f20 )>",
"<NSLayoutConstraint:0xc3c1ff0 V:[UIImageView:0xc396fc0]-(43)-[UIImageView:0xc3b7700]>",
"<NSAutoresizingMaskLayoutConstraint:0xc3bf8c0 h=--& v=--& V:[UITableViewCellContentView:0xc396f20(126.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>
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.
我认为尤其是以下部分很有趣:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc3970d0 V:[UIImageView:0xc396fc0(75)]>
所以问题是我的 UIImageview 在左上角的高度???我试图删除高度和许多其他东西,但绝对注意到对我有用....我该怎么办?我想要左上角的 UIImageView 的固定大小...
编辑: 也许问题可能是底部 UIImageView 是可选的,有时它是隐藏的,因此可能会导致警告......
我的代码的重要部分:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = @"NewsCell";
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
NSString *bottomImage = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];
if(bottomImage == (NSString *)[NSNull null] || bottomImage == nil || bottomImage.length == 0){
newsCell.bottomImage.hidden = YES;
newsCell.bottomImage.image = nil;
newsCell.bottomConstraint.constant = 0.0f;
[newsCell layoutIfNeeded];
} else {
newsCell.bottomConstraint.constant = 19.0f;
newsCell.bottomImage.hidden = NO;
[newsCell layoutIfNeeded];
[newsCell.contentView addSubview:newsCell.boardNoteImage];
}
newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"title"];
newsCell.messageText = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"previewMessage"];
NSString *authorImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"objectImage"];
return newsCell;
}
编辑 2: 我做了一些更改,现在单元格的高度计算为:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *bottomImage = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];
//Check if the bottom Image exists
if(bottomImage == (NSString *)[NSNull null] || bottomImage == nil || bottomImage.length == 0){
// if it not exists the cell should be smaller
return 128.0f;
}
//if the image exists the size should be 250
return 250;
}
编辑:3
终于现在可以使用了。我有两个解决方案。第一个是将单元格高度设置为 148.0f,老实说我不知道为什么,也许是我的 UITextView 中的一些奇怪的插图?)。而第二种解决方法是删除底部Image的底部Constraint,并为Bottom Image设置一个fix Height!
【问题讨论】:
-
要获得有效的自动布局,添加或调整项目大小时必须遵循情节提要中的蓝色指南。如果它不是真的强制,您可以禁用自动布局并使用经典的弹簧/支柱。它们更简单,更易于使用。
-
感谢您的回复,我忘了说我使用的是 xib 文件!
-
xib 的情况相同,使用蓝色指南。如果要禁用自动布局,则应在调色板(第一个检查器)中取消选中它。然后,在尺寸检查器中,您将拥有可用的弹簧/支柱
-
但自动布局是未来,所以看来我必须在桌子上敲很久才行 :)
-
@CalinChitu Interface Builder 中的蓝色指南与 AutoLayout 无关。它们纯粹是为了帮助您使用 Apple HIG 中定义的标准间隙放置对象。即距离边缘20个点。元素之间有 8 个点。等等……
标签: ios objective-c uitableview autolayout