用于更新视图以及其子视图布局,不会当即更新,是一个异步方法,须要在主线程调用。该方法将视图以及其子视图标记,在下一个更新周期执行更新操做,不知道具体更新时间。缓存
关于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;
复制代码
参数说明: