Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIView纵横比混淆了systemLayoutSizeFittingSize_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

Ios UIView纵横比混淆了systemLayoutSizeFittingSize

Ios UIView纵横比混淆了systemLayoutSizeFittingSize,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,好的,另一个UITableViewCell动态高度问题,但有点扭曲。 不幸的是,我不能跳到iOS 8只有当发布,否则问题将得到解决。需要iOS>=7.1 我试图实现一个单元格,单元格顶部有两个图像,下面有一个标题标签,下面有一个描述标签。我知道两个顶部图像将是正方形的,因此我希望它们具有相同的大小,并保持方形纵横比,但在屏幕大小变化时(如方向变化或不同的设备)调整大小 可能有助于可视化的图像(无法包括,因为rep.Nevermind颜色,可视化帮助): 注意最后一个文本后面的空白,它不应该在那

好的,另一个UITableViewCell动态高度问题,但有点扭曲。 不幸的是,我不能跳到iOS 8只有当发布,否则问题将得到解决。需要iOS>=7.1

我试图实现一个单元格,单元格顶部有两个图像,下面有一个标题标签,下面有一个描述标签。我知道两个顶部图像将是正方形的,因此我希望它们具有相同的大小,并保持方形纵横比,但在屏幕大小变化时(如方向变化或不同的设备)调整大小

可能有助于可视化的图像(无法包括,因为rep.Nevermind颜色,可视化帮助):

注意最后一个文本后面的空白,它不应该在那里

我在以前的应用程序中根据以下内容实现了动态高度: (取得了成功),现在也使用了同样的方法

我已经使用故事板和编程方式设置了约束,但没有成功

这是踢球的人。打电话时:

CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
在:

读取时,高度值远大于高度:

cell.contentView.frame
另一个问题是,如果移除图像上的纵横比约束,只将其更改为显式高度约束,则效果良好

对于任何愿意提供帮助的人,我准备了一个非常简单的示例项目来说明问题:

它设置为使用故事板进行约束,还有一个额外的分支:ExplicitHeightConstraint,它只会将图像的纵横比约束更改为高度约束

**因此,问题是:** 是否有人发现约束有任何错误,使其混淆高度计算,或者有人有任何其他建议来获得想要的结果?(尽管我想对视图使用自动布局)

作品中有相当多的限制(虽然没有什么特别的),所以我认为在提供的故事板(github链接)中查看要比在这里明确说明它们更容易。但如果有人认为我也能做到。估计高度计算如下所示:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

// Don't care about memory leaks now:
DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dynamicCell"];

[self populateCell:cell withContent:self.tableData[indexPath.row]];

// Make sure the constraints have been set up for this cell, since it may have just been created from scratch.
// Use the following lines, assuming you are setting up constraints from within the cell's updateConstraints method:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct cell height for different table view widths if the cell's height depends on its width (due to
// multi-line UILabels word wrapping, etc). We don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
// Also note, the final width of the cell may not be the width of the table view in some cases, for example when a
// section index is displayed along the right side of the table view. You must account for the reduced cell width.
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints.
// (Note that you must set the preferredMaxLayoutWidth on multi-line UILabels inside the -[layoutSubviews] method
// of the UITableViewCell subclass, or do it manually at this point before the below 2 lines!)
[cell setNeedsLayout];
[cell layoutIfNeeded];

// Get the actual height required for the cell's contentView
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

// So we can read debug values
CGRect contentFrame = cell.contentView.frame;
CGRect firstImageFrame = cell.firstArtwork.frame;
CGRect secondImageFrame = cell.secondArtwork.frame;
CGRect nameFrame = cell.name.frame;
CGRect numArtworksFrame = cell.numArtworks.frame;

// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}

- (void)populateCell:(DynamicTableViewCell*)cell withContent:(DynamicContent*)content
{
    cell.firstArtwork.image = content.firstImage;
    cell.secondArtwork.image = content.secondImage;
    cell.name.text = content.name;
    cell.numArtworks.text = [NSString stringWithFormat:@"%@ artworks", content.numImages];
}

感谢

我也遇到了同样的情况,我解决了这个问题,在给定的单元格中重写了
systemLayoutSizeFittingSize
函数,并将aspectation约束替换为高度约束。在下面的示例中,视图生命周期期间将
MediaPlayerSpectionConstraint
添加到视图中,并且将使用
MediaPlayerHeightConstraint
确定单元格的正确高度(请参见下面的代码)

所以,我用了

CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
而不是
[cell.contentView系统布局大小设置大小:

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
    self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);

    //Lets replace it with an exact height constraint (workaround).

    //To remove the Unable to simultaneously satisfy constraints. exceptions
    [self.contentView layoutIfNeeded];

    [self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
    [self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];

    [self setNeedsUpdateConstraints];

    CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];

    [self.contentView layoutIfNeeded];

    [self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
    [self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];

    [self setNeedsUpdateConstraints];

    return res;
}

看起来systemLayoutSizeFittingSize使用1x图像的固有内容大小来计算高度,而不考虑纵横比。因此,它返回的494.5高度实际上是使用图像固有大小的高度。是的,你是对的。样本中使用的许多图像具有相同的分辨率。我尝试了不同分辨率的图像,得到了不同的结果。如果有人想尝试,请使用名为SizeVariations的分支更新存储库。如果我们想适合整个图像,那么这可能是systemLayoutSizeFittingSize想要的行为。不确定如何设置约束以“尊重”UIImagesView的约束设置,而不必为尺寸管件设置明确的高度。但至少我有事情要做。感谢您指出这一点。老实说,您确实希望尝试使用视图大小正确的图像,否则可能会导致屏幕外渲染,这对滚动性能有很大影响。使用Core Animation Profiler并启用“彩色屏幕外渲染黄色”,您将了解我的意思。(请注意,你能把我的第一条评论标记为有用吗?)我不知道这一点。在应用程序中,我们从服务器获取第三方图像,因此我无法控制前面的大小(尽管它们有纵横比限制)。但是,我当然可以在客户端获取后调整它们的大小。不知道当使用内在大小和异步获取图像进行计算时,约束将如何反应。我需要研究一些东西以及性能分析。我想把这个评论标记为有帮助,但不知道怎么做。这是因为我完全不懂技术,还是与我的低代表性有关?将鼠标悬停在我发表的您喜欢的评论上,然后单击“向上”箭头。应该这样做。
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
    self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);

    //Lets replace it with an exact height constraint (workaround).

    //To remove the Unable to simultaneously satisfy constraints. exceptions
    [self.contentView layoutIfNeeded];

    [self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
    [self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];

    [self setNeedsUpdateConstraints];

    CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];

    [self.contentView layoutIfNeeded];

    [self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
    [self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];

    [self setNeedsUpdateConstraints];

    return res;
}