layoutSubviews解析

1.layoutSubviews的觸發

1、 init初始化不會觸發layoutSubviews。

2、 addSubview會觸發layoutSubviews。

3、 設置view的Frame會觸發layoutSubviews,當然前提是frame的值設置前後發生了變化。

4、 滾動一個UIScrollView會觸發layoutSubviews。

5、 旋轉Screen會觸發父UIView上的layoutSubviews事件。

6、 改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件。

7. 一個view是不能夠自己調用layoutSubviews,如果要調用,需要調用 setNeedsLayout或者 layoutIfNeeded。

8. 如果view的frame值爲0,即使被添加了耶不會調用layoutSubviews。

2.Lays out subviews文檔解析。

The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.

在iOS5.1或之前的版本中,這個方法什麼也沒幹.這個方法的默認實現是 用參數來設定subviews的尺寸和位置的 . 

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly. 

如果你需要更加精確的佈局,可以在子類裏面重寫這個方法.僅僅在以下情況下:自動佈局達不到你想要效果時你纔有必要重寫這個方法.你可以直接設置subviews的尺寸.

You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method. 

你不能直接調用這個方法.如果你需要強制layout刷新,調用setNeedsLayout來代替.如果你想要立即刷新你的view,調用layoutIfNeeded

大概總結以下就是:

你不要直接調用方法layoutSubviews,如果想要刷新,請調用 setNeedsLayout 或者 layoutIfNeeded。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章