简体   繁体   English

UIView的动态高度使用intrinsicContentSize

[英]UIView with dynamic height that uses intrinsicContentSize

I am trying to create a custom container view that has a UIImageView and a multiline UILabel as subviews. 我正在尝试创建一个自定义容器视图,其中包含UIImageView和多行UILabel作为子视图。 To make the view work nicely with autolayout, I am overriding intrinsicContentSize as below: 为了使视图与autolayout很好地协同工作,我将覆盖intrinsicContentSize,如下所示:

- (CGSize)intrinsicContentSize
{
    return [self sizeThatFits:self.bounds.size];
}

The size calculated in sizeThatFits has the same width, and adjusts the height so that the label and image are not clipped. sizeThatFits计算的大小具有相同的宽度,并调整高度,以便不剪切标签和图像。 This works well, but I was surprised to see in the docs the following comment: 这很好用,但我很惊讶地在文档中看到以下评论:

This intrinsic size must be independent of the content frame, because there's no way to dynamically communicate a changed width to the layout system based on a changed height, for example. 此内在大小必须独立于内容框架,因为例如,无法基于更改的高度动态地将更改的宽度传达给布局系统。

If that is the case, what is the autolayout way to adjust the views current height based on its width and content? 如果是这种情况,什么是自动布局方式根据其宽度和内容调整视图当前高度? Should I be approaching this in a different way? 我应该以不同的方式接近这个吗?

To answer my own question, it appears that there is not an autolayout suitable solution to this situation. 为了回答我自己的问题,似乎没有针对这种情况的自动布局合适的解决方案。 Looking to UILabel for inspiration, the problem here has been solved with the addition of a property preferredMaxLayoutWidth , which can then be used as a constraining width during the intrinsic content size calculation. 期待UILabel的灵感,这里的问题已经通过添加属性preferredMaxLayoutWidth来解决,该属性可以在内在内容大小计算期间用作约束宽度。 Any custom view would need to use something similar. 任何自定义视图都需要使用类似的东西。

I think the doc means that, your containerView might have a placeHolderFrame as content frame. 我认为doc意味着你的containerView可能有一个placeHolderFrame作为内容框架。

intrinsic size should not be related to the content frame, but only to it's own subContent. 内在大小不应与内容框架相关,而只与其自身的子内容相关。 Your image and UILabel for example. 例如你的形象和UILabel。

You should calculate both height and the width from the label and the image. 您应该从标签和图像计算高度和宽度。 Which should be easy, since they all have intrinsic size. 哪个应该很容易,因为它们都有内在的大小。

Just my opinion... 只是我的观点...

I guess you could use UILabel's new preferredMaxLayoutWidth property to layout label correctly and use other approaches to layout other stuff. 我想你可以使用UILabel的新preferredMaxLayoutWidth属性来正确布局标签,并使用其他方法来布局其他东西。

Something like this: 像这样的东西:

- (void)layoutSubviews
{
    ...
    [super layoutSubviews]; // get width from solved constraints
    label.preferredMaxLayoutWidth = label.frame.size.width; // use it
    [super layoutSubviews]; // update height of a label (probably intrinsicContentSize)
    ...
}

Align the bottom edge of the containing view with both the image and the label. 将包含视图的下边缘与图像和标签对齐。

[self alignBottomEdgeWithView:labelView predicate:@"10"];

Details in http://code.dblock.org/ios-uiview-with-an-image-and-text-with-dynamic-height . 详细信息请参见http://code.dblock.org/ios-uiview-with-an-image-and-text-with-dynamic-height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM