對Auto Layout中的Content Compression Resistance和Content Hugging的總結

轉自---- 這篇講的很通俗易懂,消滅盲點

http://codingobjc.com/blog/2015/01/28/autolayoutzhong-de-content-compression-resistancehe-content-huggingdao-di-shi-shi-yao-yi-si/


Auto Layout中,Content Compression Resistance Content Hugging這兩個概念,從字面上很難理解它們真正的意思和用途。中文世界也沒有與之對應的特別形象的中文翻譯。objc中國上,將其分別翻譯爲內容壓縮阻力和內容吸附性,這可能一定程度上更加深了理解的難度。而且也很難在蘋果官方文檔中找到明確的相關描述(至少在比較綜合性的論述Auto Layout系統機制的文檔《Auto Layout Guide》上是沒有找到明確的描述)。

對於這兩個概念,我也一直是一知半解。直到最近搜索和查看了許多文章和資料才徹底搞清楚了它們的作用,所以在此整理一下。

Intrinsic Content Size

要理解內容壓縮阻力和內容吸附性這兩個概念,首先要理解固有內容尺寸(Intrinsic Content Size)這一概念。

每個視圖都有內容壓縮阻力優先級(Content Compression Resistance Priority)和內容吸附性優先級(Content Hugging Priority)。但只有當視圖定義了固有內容尺寸後,這兩種優先級纔會起作用;否則如果都沒有定義內容尺寸大小,又如何知道應該抗壓縮或者吸附至什麼大小呢?對吧?

那麼,固有尺寸大小又是指的什麼,有什麼作用呢?

引用自蘋果官方Auto Layout指南里面對固有內容尺寸的描述:


Intrinsic Content Size

Leaf-level views such as buttons typically know more about what size they should be than does the code that is positioning them. This is communicated through the intrinsic content size, which tells the layout system that a view contains some content that it doesn’t natively understand, and indicates how large that content is, intrinsically.

For elements such as text labels, you should typically set the element to be its intrinsic size (select Editor > Size To Fit Content). This means that the element will grow and shrink appropriately with different content for different languages. `

什麼意思呢?

簡單來說就是,像按鈕、文本標籤這類視圖控件,在佈局的時候,它們自己內部比外部佈局代碼更清楚自己需要多大的尺寸來顯示自己的內容。而這個尺寸就是由固有內容尺寸(intrinsic content size)來傳達的。這就相當於,固有內容尺寸告訴佈局系統:這個視圖包含了一些你天生不能理解的內容,但是我給你指出了那些內容有多大,固有的尺寸。

由此可見,固有內容尺寸是爲了實現視圖自適應大小而準備的。

Content Compression Resistance 與 Content Hugging

其實,關於這兩個概念,最容易理解的文檔說明就在UIView Class Reference文檔裏面:

- contentCompressionResistancePriorityForAxis:

Returns the priority with which a view resists being made smaller than its intrinsic size.

- contentHuggingPriorityForAxis:

Returns the priority with which a view resists being made larger than its intrinsic size.

參照以上兩個接口的說明,其實意義已經相當清楚了。簡單點來說,內容壓縮阻力優先級就是視圖反壓縮的優先級,優先級越大,視圖就越不容易被壓小;內容吸附性優先級就是視圖反拉伸的優先級,優先級越大,視圖就越不容易被拉大。

例子

下面,引用一個來自stackoverflow的例子,這個例子很形象的解釋了內容壓縮阻力和內容吸附性優先級的作用。

假設,你有一個下面這樣的按鈕:

1
[		Click Me		]

按鈕與其父視圖之間的邊距約束優先級是500。

然後,如果按鈕的吸附性優先級(Hugging priority)大於500,按鈕看起來會是這樣:

1
[Click Me]

如果,吸附性優先級小於500,按鈕會是這樣:

1
[		Click Me		]

然後,如果現在父視圖收縮了,按鈕的壓縮阻力優先級(Compression Resistance priority)大於500,它看起來會是這樣:

1
[Click Me]

否則,如果壓縮阻力優先級小於500,它會是這樣:

1
[Cli..]

如果不是這樣,則很可能是有一些其他的約束擾亂了你的整個佈局!例如,可能你的邊距約束優先級是1000。或者你可能有一個優先級較高的寬度約束。如果遇到這種情況,可以試試“Editor > Size to Fit Content”菜單命令。

總結

因此,我們可以簡單總結爲:

  1. Content Hugging:反拉伸
  2. Content Compression Resistance:反壓縮
  3. 僅當視圖定義了自己的Intrinsic Content Size,那麼它的Content Compression Resistance優先級和Content Hugging優先級屬性纔有作用。

參考

  1. Auto Layout Guide
  2. UIView Class Reference
  3. Advanced Auto Layout Toolbox
  4. Cocoa Autolayout: content hugging vs content compression resistance priority
發佈了65 篇原創文章 · 獲贊 11 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章