没有图像的Autolayout UIImageView

时间:2014-03-07 14:15:50

标签: ios cocoa-touch uiimageview autolayout

我有一个带有缩放中心的UIImageView,所以它的大小根据内部的UIImage而变化,但是,通过设置一个nil UIImage,约束会中断,因为没有内容大小可以计算

如何使用nil UIImage处理自动布局(大小应更改为0,0)?

2 个答案:

答案 0 :(得分:16)

问题是UIImageView在没有设置图像时返回{-1,-1}作为intrinsicContentSize。使用UIImageView的子类,实现如下:

@implementation MyImageView

- (CGSize)intrinsicContentSize
{
    if (self.image)
    {
        return [super intrinsicContentSize];
    }

    return CGSizeZero;
}

@end

答案 1 :(得分:0)

Content hugging priority设为Required(1000)。

更新:这个答案之前有效,但现在还没有用。 使用Nikita Ivaniushchenko 代替。