自动布局隐藏第一次启动的图像

Posted

技术标签:

【中文标题】自动布局隐藏第一次启动的图像【英文标题】:Autolayout hides Image for first start 【发布时间】: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 个点。等等…… 【参考方案1】:

您当前的约束设置将使底部UIImageView 的高度为-1.5。

自动布局的第二条规则——你不能有否定!

您可以从错误消息中看到这一点。

从上到下(UITableViewCell)你有......

10 的差距。 身高 75。 43 的差距。 可变高度(这是大图) 0 的差距。

这加起来是 128。

现在,单元格从上到下的总高度也为 126.5。

满足这些要求的唯一方法是将图像视图的可变高度设置为 -1.5。 这不能做 它违反了 AutoLayout 的第二条规则。因此,它选择了一个它认为可能有帮助的约束并将其删除。因此,您可能会获得 0 或其他值的图像视图高度。

您需要更改约束或使单元格高度为 128。

【讨论】:

非常感谢您的回复,这似乎是问题所在,我试图将单元格高度更改为 128,但现在我收到相同的警告,它说:“<0xc576ec0 h="-" v="--&" v:> 【参考方案2】:

我在一个示例项目中复制了您的布局,我可以重现您的问题的唯一方法是在 cellForRowAtIndexPath 中进行设置:

cell.translatesAutoresizingMaskIntoConstraints = NO;

如果您这样做,请尝试将其删除。我没有时间深入研究细节以了解为什么这会导致布局受到过度约束。但总的来说,我认为一个好的经验法则是不要更改此默认值,因为表格视图拥有单元格本身的布局。您只拥有内容的布局。

【讨论】:

感谢您的回复,遗憾的是我从未使用过。也许原因是底部的可选 UIImageView(请参阅我的编辑)

以上是关于自动布局隐藏第一次启动的图像的主要内容,如果未能解决你的问题,请参考以下文章

在自动布局中方向更改为横向时隐藏视图

使用带有自动布局的推送时隐藏按钮时,约束更新导致项目移动

表格视图中单元格内的自动布局图像。正确的布局,但只有一次向下滚动和备份?

约束/自动布局栏隐藏,Xcode 6

使用自动布局并创建动态界面(隐藏字段时自动重新对齐)

自动布局是拉伸图像,自动布局,ios