I don't know where the original source is. Here we will go down. Thank you!
Layoutsubviews Summary
IOS layout Mechanism
-(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. Initialization of init will not trigger layoutsubviews
However, when initwithframe is used for initialization, when the rect value is not cgrectzero
2. addsubview triggers layoutsubviews
3. Setting the View frame triggers layoutsubviews. Of course, the premise is that the frame value is changed before and after the frame value is set.
4. Rolling a uiscrollview triggers layoutsubviews.
5. Rotating Screen triggers the layoutsubviews event on the parent uiview.
6. Changing the size of a uiview triggers the layoutsubviews event on the parent uiview.
In Apple's official documents, I emphasized:
You shoshould override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.
Layoutsubviews, which must be called when the position of the subview is adjusted within a class.
This means that if you want to set the location of the subviews externally, do not rewrite it.
Refresh sub-object Layout
-Layoutsubviews method: This method does not do anything by default and must be rewritten by subclass.
-Setneedslayout: The layoutifneeded method is called asynchronously to refresh the layout. If the layout is not refreshed immediately, layoutsubviews will be called.
-Layoutifneeded method: if there are tags to be refreshed, call layoutsubviews to layout immediately (if there is no tag, layoutsubviews will not be called)
If you want to refresh the page immediately, call [view setneedslayout], set the flag to layout, and call [view layoutifneeded] immediately to implement layout.
Before the view is displayed for the first time, you can directly call [view layoutifneeded]
Redraw
-Drawrect :( cgrect) rect method: rewrite this method to execute the re-painting task.
-Setneedsdisplay method: mark it as re-painting required and call drawrect Asynchronously
-Setneedsdisplayinrect :( cgrect) invalidrect method: Mark as partial repainting required
Sizetofit will automatically call the sizethatfits method;
Sizetofit should not be overwritten in the subclass, and should be rewritten in sizethatfits
The input parameter sizethatfits is the current size of the aggreger, and an appropriate size is returned.
Sizetofit can be manually called directly
Neither the sizetofit nor the sizethatfits method is recursive, and it is not responsible for subviews. It is only responsible for itself.
------------
Layoutsubviews re-layout subviews
Layoutsubviews method call prior to drawrect
Setneedslayout indicates a tag that needs to be re-laid on the worker er. It automatically calls layoutsubviews in the next cycle of the system runloop.
If the layoutifneeded method is its name, uikit will determine whether the handler requires layout. According to the official Apple documentation, the layoutifneeded method should be like this
Layoutifneeded does not traverse superview chains, but should be subviews chains.
Drawrect is a re-painting of the receiver, which can obtain the context
Setneeddisplay indicates a tag to be re-drawn on the worker er. It is automatically re-painted in the next draw cycle. The refresh frequency of the iPhone device is 60Hz, that is, re-painting after 1/60 seconds.