UIStackView错误计算UIImageView高度

时间:2015-10-27 13:43:23

标签: ios objective-c uiimageview autolayout uistackview

我在UIStackView中包含UICollectionViewCell来代表社交网络上的帖子(与Instagram的小区类似)。 UIStackView包含作者标题(自定义UIView),内容图片UIImageView,帖子正文UILabelUIToolbar动作。最终视图看起来像this

通过设置大小调整单元格来设置UICollectionViewCell的高度,如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      NTVFeedBlogCollectionViewCell *cell = [[NTVFeedBlogCollectionViewCell alloc] initWithFrame:CGRectMake(10.0f, 0.0f, collectionView.frame.size.width - 20.0f, 900000.0)];

      // ...
      // Configure the cell
      // ...

      [cell setNeedsLayout];
      [cell layoutIfNeeded];
      CGSize size = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

      return CGSizeMake(CGRectGetWidth(collectionView.frame) - 20.0f, size.height);

}

问题是UIImageView似乎增加了UICollectionViewCell的大小,没有增加图像视图的大小。我将大图像添加到图像视图时This is the result。所需的结果是图像保持1:1的宽高比,受限于UIStackView的宽度。

正文标签与其他内容之间的间隙大小会根据我使用的图像而变化。这让我相信UIStackView仅以某种方式考虑了单元格的大小调整的图像大小,因为在视觉上图像视图的大小正确。正如您在第一张贴图中看到的那样,当图像视图的图像尚未设置时,单元格完美展现。

以下是堆栈视图,图像视图和标签的设置代码:

self.stackView = [[UIStackView alloc] initWithFrame:CGRectZero];
self.stackView.translatesAutoresizingMaskIntoConstraints = NO;
self.stackView.axis = UILayoutConstraintAxisVertical;
self.stackView.distribution = UIStackViewDistributionFill;
self.stackView.alignment = UIStackViewAlignmentFill;
self.stackView.spacing = 10.0f;
self.stackView.layoutMargins = UIEdgeInsetsMake(10.0f, 10.0f, 0.0f, 10.0f);
self.stackView.layoutMarginsRelativeArrangement = YES;

[self addSubview:self.stackView];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[stackView]-0-|"
                                                             options:0
                                                             metrics:nil
                                                               views:@{@"stackView": self.stackView}]];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[stackView]-0-|"
                                                             options:0
                                                             metrics:nil
                                                               views:@{@"stackView": self.stackView}]];

self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];

[self.imageView setImage:[UIImage imageNamed:@"sample_image.png"]];

self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
self.imageView.layer.masksToBounds = YES;
self.imageView.backgroundColor = [UIColor greenColor];

[self.imageView setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[self.imageView setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];

[self.stackView addArrangedSubview:self.imageView];

[self.stackView addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView
                                                           attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:self.stackView
                                                           attribute:NSLayoutAttributeWidth
                                                          multiplier:1.0f
                                                            constant:0.0f]];

self.bodyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.bodyLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
self.bodyLabel.font = [UIFont ntv_lightFontWithSize:17.0f];
self.bodyLabel.textColor = [UIColor whiteColor];
self.bodyLabel.numberOfLines = 0;

self.bodyLabel.text = @"While the stack view allows you to layout its contents without using Auto Layout directly, you still need to use Auto Layout to position the stack view, itself.";
[self.stackView addArrangedSubview:self.bodyLabel];

self.accessoryView = [[NTVAccessoryView alloc] initWithFrame:CGRectZero];
self.accessoryView.viewModel = [NTVAccessoryManagerViewModel_Stubbed new];

[self.stackView addArrangedSubview:self.accessoryView];

有什么想法吗?

2 个答案:

答案 0 :(得分:8)

我们想通了!如文档中所述,UIStackView正在使用其intrinsicContentSize的{​​{1}}来计算其高度。

arrangedSubviews报告其UIImageView作为图片的整体尺寸(如您所料)。 intrinsicContentSize使用图片视图的UIStackView忽略了我的身高限制,而只是

为了解决这个问题,我创建了一个intrinsicContentSize子类,允许调用者设置UIImageView。可以找到此代码here

然后,在我的intrinsicContentSize我正在设置UIImageView子类'布局传递完成后UICollectionViewCell

intrinsicContentSize

}

答案 1 :(得分:0)

如果您不想以编程方式执行此操作,则另一个解决方案是垂直使用2个UIStackView。包含UIImageView的顶级UIStackView设置其高度约束,比如超视图高度的70%,并让底部UIStackView紧靠顶部一个,所以它将占据剩余的30%高度。

通过这种方式,无论你的图像有多大,它总是占据高度的70%。