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

问题描述:

我有一个UIImageView(它实际上是一个FLAnimatedImageView)。我使用自动布局来设置其约束以占据整个屏幕。这在iPhone中运行良好,但在iPad中,内容的自动创建约束(如图所示)使其仅占用屏幕的一部分尺寸。UIImageView自动创建的内容大小自动布局约束与尾随/领先约束重叠

我不明白为什么会产生这些约束,如果确实需要这些约束,为什么不遵守尾随/领先约束?

constraints

The image only taking some of the screen's size

我GOOGLE了你的观点的来源,发现它在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; 
} 

看来,在动画原图的情况下,大小将减少到原来的图像尺寸。

苹果文档讲述内在含量的大小:

在一般情况下,固有的内容大小简化了布局,减少限制的数量,您需要

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

一种解决方案可能是(不知道是否有一些副作用)来使用,它覆盖的内在内容的大小就像一个子类:BTW

override var intrinsicContentSize: CGSize { 
    return CGSize(width: UIViewNoIntrinsicMetric, height: UIViewNoIntrinsicMetric) 
} 

:请在下一个问题中提供一个链接,以便我们节省时间搜索它。谢谢!

虽然接受的答案确实为这些自动约束条件不能创建提供了一个解决方案,但导致我的观点被削减的问题是不同的。

我在做的视图设置(不是通过故事板),但视图边界的更改不会生效。

viewDidLayoutSubviews中实现该逻辑(例如,在图像视图的中心添加视图,例如进度条的视图,该视图依赖于图像视图的宽度)给了我一个入口点,约束生效,因此UIImageView宽度为报告正确地在该点查看frame.size.width

viewDidLayoutSubviews官方文档:

当界限的视图控制器的看法改变,视图调整其子视图的位置,然后系统会调用这个方法

来源:Apple Docs