Ios 自动布局为第一次启动隐藏图像
我在使用xib文件创建的自定义TableViewCell中使用autolayout,如下所示: 一切正常,显示正确,但我在向下滚动TableView时收到很多警告消息: 无法同时满足约束。 可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档) ( 编辑2: 我做了一些更改,现在单元格的高度计算为:Ios 自动布局为第一次启动隐藏图像,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我在使用xib文件创建的自定义TableViewCell中使用autolayout,如下所示: 一切正常,显示正确,但我在向下滚动TableView时收到很多警告消息: 无法同时满足约束。 可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizi
-(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中有一些奇怪的插入?)。第二种解决方案是删除底部图像的底部约束,并为底部图像设置固定高度 我在一个示例项目中复制了您的布局,复制您的问题的唯一方法是在
cellforrowatinexpath
中设置它:
cell.translatesAutoresizingMaskIntoConstraints = NO;
如果要这样做,请尝试将其移除。我还没有时间深入了解细节,以理解为什么这会导致布局受到过度约束。但一般来说,我认为一个好的经验法则是不要更改这个默认值,因为表视图拥有单元格本身的布局。您只拥有内容的布局。您当前的约束设置将使底部
UIImageView
的高度为-1.5
自动布局的第二条规则-你不能有底片强>
您可以从错误消息中看到这一点
从上到下(UITableViewCell)您有
- 10的差距
- 75英尺高
- 差距为43
- 可变高度(这是大图像视图)
- 0的差距
您需要更改约束条件或将单元格高度设置为128。要使用自动布局,在添加或调整项目大小时,必须遵循故事板中的蓝色指导原则。如果不是强制的,可以禁用autolayout并使用经典的弹簧/支柱。它们更简单,更容易使用。感谢您的回复,我忘了说我正在使用xib文件!对于xib,同样的情况,使用蓝色指南。若要禁用自动布局,应在调色板(第一个检查器)中取消选中它。然后,在size inspector中,您将拥有可用的弹簧/支柱,但自动布局是未来的趋势,因此我似乎必须在桌面上长时间敲打我的头,直到它起作用:)@Calinchiu Interface Builder中的蓝色指南与自动布局无关。它们纯粹是为了帮助您使用Apple HIG中定义的标准间隙放置对象。i、 e.距离边缘20点。元素之间有8个点。等等…谢谢你的回复,很遗憾我从来没有用过。可能原因是底部的可选UIImageView(请参见我的编辑)非常感谢您的回答,这似乎是个问题,我试图将单元格高度更改为128,但现在我收到了相同的警告,它说:“@Davis您是如何更改单元格高度的?”?更改单元格高度的正确方法是更改
tableView:heightforrowatinexpath:
(CGFloat)tableView:(UITableView*)tableView heightforrowatinexpath:(nsindepath*)indexath{return 128;}@Davis OK中的返回值。您正在更改的newsCell.bottomConstraint
是什么?它限制了什么?这是底部图像视图的高度吗?还是别的什么?另外,试着为我将高度设置为150(看看会发生什么)。@Davis实际上,这可能与分隔线的默认插入有关。i、 e.您将单元格高度设置为128,但cell.contentView高度会降低以适应tyne分隔线。尝试更大的值(129应该可以)。
- (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;
}
-(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;
}
cell.translatesAutoresizingMaskIntoConstraints = NO;