用於更新視圖以及其子視圖佈局,不會當即更新,是一個異步方法,須要在主線程調用。該方法將視圖以及其子視圖標記,在下一個更新週期執行更新操做,不知道具體更新時間。緩存
關於setNeedsLayout
,官方文檔描述以下:app
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.異步
用於更新視圖以及其子視圖佈局,當即更新,不會等待更新週期,從根視圖開始更新視圖子樹,若無待更新的佈局,直接退出。ide
關於layoutIfNeeded
,官方文檔描述以下:佈局
Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root. If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.動畫
通常是在autoresizing
或者 constraint-based
的狀況下重寫此方法。經常使用場景是當視圖不是使用frame初始化的時候,咱們能夠在此方法進行一些精細化佈局。如子視圖相對於父視圖使用約束佈局,子視圖init時,不具備frame直到約束創建,由於不知道約束創建完成時機,而咱們又確實須要frame進行一些計算,爲確保計算時拿到的frame不爲空,此時,咱們能夠將計算的過程放於此,並配合setNeedsLayout
或者layoutIfNeeded
進行視圖刷新。ui
關於layoutSubviews
,官方文檔描述以下:this
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.spa
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.線程
每次訪問UIViewController的view(好比controller.view、self.view)並且view爲nil,loadView方法就會被調用。
loadView方法是用來負責建立UIViewController的view
默認實現即[super loadView]
裏面作了什麼事情。
[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
複製代碼
[[MyViewController alloc] init]; // 加載MyViewController.xib
複製代碼
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
複製代碼
[super loadView]
裏面就大體完成a)和b)中敘述的內容
Tips:
若要本身重寫loadView
方法,此時爲節省開銷,應避免調用[super loadView]
方法。
按照執行順序排列:
init
初始化ViewControllerloadView
當view須要被展現而它倒是nil時,viewController會調用該方法。若是代碼維護View的話須要重寫此方法,使用xib維護View的話不用重寫。viewDidLoad
執行完loadView後繼續執行viewDidLoad,loadView時尚未view,而viewDidLoad時view已經建立好了。viewWillAppear
視圖將出如今屏幕以前,立刻這個視圖就會被展示在屏幕上了;viewDidAppear
視圖已在屏幕上渲染完成 當一個視圖被移除屏幕而且銷燬的時候的執行順序,這個順序差很少和上面的相反;viewWillDisappear
視圖將被從屏幕上移除以前執行viewDidDisappear
視圖已經被從屏幕上移除,用戶看不到這個視圖了viewWillUnload
若是當前有能被釋放的view,系統會調用viewWillUnload方法來釋放viewviewDidUnload
當系統內存吃緊的時候會調用該方法,在iOS 3.0以前didReceiveMemoryWarning是釋放無用內存的惟一方式,可是iOS 3.0及之後viewDidUnload方法是更好的方式。在該方法中將全部IBOutlet(不管是property仍是實例變量)置爲nil(系統release view時已經將其release掉了)。在該方法中釋放其餘與view有關的對象、其餘在運行時建立(但非系統必須)的對象、在viewDidLoad中被建立的對象、緩存數據等。通常認爲viewDidUnload是viewDidLoad的鏡像,由於當view被從新請求時,viewDidLoad還會從新被執行。dealloc
視圖被銷燬,此處須要對你在init和viewDidLoad中建立的對象進行釋放.關於viewDidUnload :在發生內存警告的時候若是本視圖不是當前屏幕上正在顯示的視圖的話,viewDidUnload將會被執行,本視圖的全部子視圖將被銷燬以釋放內存,此時開發者須要手動對viewLoad、viewDidLoad中建立的對象釋放內存。由於當這個視圖再次顯示在屏幕上的時候,viewLoad、viewDidLoad 再次被調用,以便再次構造視圖。基本用法:
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
複製代碼
參數說明: