LayoutSubviews summary (role and call mechanism)

IOS layout mechanism correlation method

- (CGSize) sizeThatFits: (CGSize) size - (void) - sizeToFit - (void) - layoutSubviews (void) layoutIfNeeded - (void) - setNeedsLayout - (void) - setNeedsDisplay (void) drawRect

LayoutSubviews will be called in the following cases:

1, init initialization does not trigger layoutSubviews
but is initialized with initWithFrame, when the rect value is not CGRectZero, it will also trigger 2, addSubview 3, will trigger the layoutSubviews set view Frame triggers layoutSubviews, of course, the premise is to set the frame value before and after the change in 4, rolling a UIScrollView will trigger layoutSubviews 5, the rotating Screen will trigger the parent UIView the layoutSubviews event on the 6, when a change in the size of UIView will trigger the layoutSubviews event on the parent UIView

In Apple’s official document:

You should override method if the behaviors of subviews do not offer the the behavior you want. autoresizing only this

LayoutSubviews, we need to call when we adjust the sub – view position within a class.
, in turn, means that if you want to set the subviews position on the outside, don’t rewrite it.

Refresh child object layout

Methods: the -layoutSubviews method, the default did not do anything to the subclass overrides the -setNeedsLayout method: marked for re layout, asynchronous call layoutIfNeeded to refresh the layout, not immediately refresh, but layoutSubviews will be called -layoutIfNeeded: if there is need to refresh, mark, immediately call the layoutSubviews layout (if there is no mark, not call layoutSubviews)

If you want to refresh immediately, you must first call the [view setNeedsLayout], set the label to the layout, and then call [view layoutIfNeeded], to achieve the layout

Before the first display of the view, the tag always needs to refresh, you can directly call [view layoutIfNeeded]


-drawRect: (CGRect) rect method: override this method to redraw the execution task
-setNeedsDisplay method: labeled to be redrawn, asynchronous swap with drawRect (CGRect)
-setNeedsDisplayInRect: invalidRect method: labeled partial redraw

SizeToFit automatically calls the sizeThatFits method;
sizeToFit should not be overridden in a subclass should override the sizeThatFits
parameter is receiver sizeThatFits to the current size, return a suitable size

SizeToFit can be called directly by the
sizeToFit and sizeThatFits methods are not recursive, not responsible for subviews, only responsible for their own

— — — — —

LayoutSubviews subviews re layout
layoutSubviews method calls before drawRect

SetNeedsLayout on the receiver mark a need to be re layout of the tag, the next cycle of the system runloop automatically calls layoutSubviews

LayoutIfNeeded method as its name, UIKit will determine whether the receiver needs layout. according to the official Apple document, layoutIfNeeded method should be like this

LayoutIfNeeded traversal is not superview chain, it should be subviews chain

DrawRect is a redraw of receiver, can get context

SetNeedDisplay on the receiver marked with a need to be re drawing of the mark, in the next draw cycle automatically redraw, iPhone device refresh rate is 60Hz, that is, 1/60 seconds after redraw