UIImageView 自动创建的内容大小自动布局约束与尾随/前导约束重叠

Sha*_*har 2 xcode ios autolayout swift

我有一个 UIImageView (它实际上是一个 FLAnimatedImageView)。我使用自动布局来设置其约束以占据整个屏幕。这在 iPhone 中效果很好,但在 iPad 中,自动创建的内容限制(如图所示)导致它只占用部分屏幕尺寸。

我不明白为什么要创建这些约束,如果确实需要它们,为什么不遵守尾随/前导约束?

限制条件

图像仅占据屏幕的部分尺寸

Obj*_*ist 5

我用谷歌搜索了你的观点的来源,并在GitHub上找到了它。在研究了实现之后我发现了以下代码片段:

- (CGSize)intrinsicContentSize
{
    // Default to let UIImageView handle the sizing of its image, and anything else it might consider.
    CGSize intrinsicContentSize = [super intrinsicContentSize];

    // If we have have an animated image, use its image size.
    // UIImageView's intrinsic content size seems to be the size of its image. The obvious approach, simply calling `-invalidateIntrinsicContentSize` when setting an animated image, results in UIImageView steadfastly returning `{UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric}` for its intrinsicContentSize.
    // (Perhaps UIImageView bypasses its `-image` getter in its implementation of `-intrinsicContentSize`, as `-image` is not called after calling `-invalidateIntrinsicContentSize`.)
    if (self.animatedImage) {
        intrinsicContentSize = self.image.size;
    }

    return intrinsicContentSize;
}
Run Code Online (Sandbox Code Playgroud)

似乎在动画图像的情况下,尺寸将减小到原始图像尺寸。

苹果文档讲述了内在内容的大小:

一般来说,固有内容大小简化了布局,减少了所需的约束数量

这可以解释为内在内容大小的使用已经增加了大小限制。有关内在内容大小使用的更多信息,请参阅:Apple 文档

一种解决方案可能是(不确定是否有一些副作用)使用覆盖内部内容大小的子类,如下所示:

override var intrinsicContentSize: CGSize {
    return CGSize(width: UIViewNoIntrinsicMetric, height: UIViewNoIntrinsicMetric)
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句:请在下一个问题中提供来源的链接,这可以节省我们搜索的时间。谢谢!