2017-04-19 62 views
0

我有一個UIImageView(它實際上是一個FLAnimatedImageView)。我使用自動佈局來設置其約束以佔據整個屏幕。這在iPhone中運行良好,但在iPad中,內容的自動創建約束(如圖所示)使其僅佔用屏幕的一部分尺寸。UIImageView自動創建的內容大小自動佈局約束與尾隨/領先約束重疊

我不明白爲什麼會產生這些約束,如果確實需要這些約束,爲什麼不遵守尾隨/領先約束?

constraints

The image only taking some of the screen's size

回答

1

我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) 
} 

:請在下一個問題中提供一個鏈接,以便我們節省時間搜索它。謝謝!

0

雖然接受的答案確實爲這些自動約束條件不能創建提供了一個解決方案,但導致我的觀點被削減的問題是不同的。

我在做的視圖設置(不是通過故事板),但視圖邊界的更改不會生效。

viewDidLayoutSubviews中實現該邏輯(例如,在圖像視圖的中心添加視圖,例如進度條的視圖,該視圖依賴於圖像視圖的寬度)給了我一個入口點,約束生效,因此UIImageView寬度爲報告正確地在該點查看frame.size.width

viewDidLayoutSubviews官方文檔:

當界限的視圖控制器的看法改變,視圖調整其子視圖的位置,然後系統會調用這個方法

來源:Apple Docs