gpt4 book ai didi

ios - 具有使用 intrinsicContentSize 的动态高度的 UIView

转载 作者:技术小花猫 更新时间:2023-10-29 10:51:46 31 4
gpt4 key购买 nike

我正在尝试创建一个自定义容器 View ,它有一个 UIImageView 和一个多行 UILabel 作为 subview 。为了使 View 与自动布局很好地配合使用,我重写了 intrinsicContentSize,如下所示:

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

sizeThatFits中计算出的size宽度相同,并调整高度使label和image不被裁剪。这很好用,但我很惊讶地在文档中看到以下评论:

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.

如果是这样,根据宽度和内容调整 View 当前高度的自动布局方式是什么?我应该以不同的方式处理这个问题吗?

最佳答案

为了回答我自己的问题,似乎没有适合这种情况的自动布局解决方案。从 UILabel 中寻找灵感,这里的问题已通过添加属性 preferredMaxLayoutWidth 得到解决,然后可以在计算内部内容大小时将其用作约束宽度。任何自定义 View 都需要使用类似的东西。

关于ios - 具有使用 intrinsicContentSize 的动态高度的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054907/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com