iOS 談談layoutSubviews何用

下面列出View的layout的方法:
layoutSubviews
layoutIfNeeded
setNeedsLayout
setNeedsDisplay
drawRect
sizeThatFits
sizeToFit

我們這里抽取常用的幾個進行驗證學習

  • 1、layoutSubviews
    -- layoutSubviews在何時會被調用呢??
下面是蘋果大大告訴我的 我也這么告訴你:
Discussion
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.

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.

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.

這個方法,默認沒有做任何事情,需要子類進行重寫 。 系統(tǒng)在很多時候會去調用這個方法,初始化不會觸發(fā)layoutSubviews,但是如果設置了不為CGRectZero的frame的時候就會觸發(fā)。

1.直接調用[self setNeedsLayout];(這個在上面蘋果官方文檔里有說明)
2.addSubview的時候。
3.當view的size發(fā)生改變的時候,前提是frame的值設置前后發(fā)生了變化。
4.滑動UIScrollView的時候。
5.旋轉Screen會觸發(fā)父UIView上的layoutSubviews事件(這個我驗證了一下 確實沒有觸發(fā)layoutSubviews方法,查了很多資料都說會觸發(fā),大家自己定奪)。

下面來驗證一下分析分析:

自定義一個DLView繼承自UIView
@implementation DLView
- (void)layoutSubviews {
    NSLog(@"來了");
}

初始化的時候不設置frame,直接addSubView會調用一次,如圖:


addsubview.png

初始化的時候設置frame為Zero
zero

,然后addSubView會調用兩次,如圖:

初始化的時候設置frame,然后addSubView會調用兩次,如圖:


兩次.png

頁面上創(chuàng)建一個button 點擊改變view的frame,點擊一次后 只要frame跟原來的frame有變化 就會調用一次layoutSubviews,再次點擊就不會調用了,除非再次改變frame不一樣的值:


改變frame

補充:當我們自定義view的時候重寫UIView的layoutSubviews如果程序需要對該控件所包含的子控件布局進行更精確的控制可通過重寫該方法來實現。

  • 2、layoutIfNeeded
    也就是使用約束的時候 調一下可以立即更新效果
    setNeedsLayout方法并不會立即刷新,立即刷新需要調用layoutIfNeeded方法!
蘋果又告訴了我:
Before displaying a window that uses constraints-based layout the system invokes this method to ensure that the layout of all views is up to date. This method updates the layout if needed, first invoking updateConstraintsIfNeeded to ensure that all constraints are up to date. This method is called automatically by the system, but may be invoked manually if you need to examine the most up to date layout.
Subclasses should not override this method
翻譯:
在顯示使用基于約束的布局的窗口之前,系統(tǒng)調用此方法以確保所有視圖的布局是最新的。這種方法更新布局,如果需要的話,首先調用updateconstraintsifneeded確保所有的約束都是最新的。該方法由系統(tǒng)自動調用,但如果需要檢查最新的布局,則可以手動調用該方法。
子類不應覆蓋此方法。
  • 3、setNeedsDisplay
    與setNeedsLayout方法相似的方法是setNeedsDisplay方法。該方法在調用時,會自動調用drawRect方法。drawRect方法主要用來畫圖。所以,當需要刷新布局時,用setNeedsLayOut方法;當需要重新繪畫時,調用setNeedsDisplay方法。

  • 4、sizeThatFits、sizeToFit
    一般在使用UILabel的時候會用到,使用這兩個方法之前,必須要給label賦值,否則不會顯示內容的。

[testLabel sizeThatFits:CGSizeMake(20, 20)];//會計算出最優(yōu)的 size 但是不會改變 自己的 size,個人認為這個就是 label 自適應大小有用別的沒什么用
[testLabel sizeToFit];//會計算出最優(yōu)的 size 而且會改變自己的size

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容