- (void)layoutSubviews;
Summary
Lays out subviews.Override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
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.
-(void)setNeedsLayout
Summary
Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
Discussion
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.
-(void)layoutIfNeeded
Summary
Lays out the subviews immediately, if layout updates are pending.Allows you to perform layout before the drawing cycle happens. -layoutIfNeeded forces layout early
Discussion
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.
setNeedsLayout和layoutIfNeeded的区别
setNeedsLayout和layoutIfNeeded都能调用layoutSubviews,但是区别在于
setNeedsLayout方法并不会立即调用layoutSubviews,而是把当前布局标记位失效布局,并且需要重新布局,等待下一个刷新循环进行刷新。好处是可以多个布局更新之后统一在一个刷新里面刷新,有利于提高性能。在这里layoutSubviews最终都是会被调用的。
而layoutIfNeeded方法会判断是否有需要刷新的标记,实际上就是布局有没有更新,如果有就立即调用layoutSubviews更新布局;如果没有,就不会调用layoutSubviews。
-(void)setNeedsDisplay;
Summary
Marks the receiver’s entire bounds rectangle as needing to be redrawn.
Discussion
You can use this method or the setNeedsDisplayInRect: to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.
If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.
You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.