iOS layout mechanism related methods
- -(Cgsize) Sizethatfits: (cgsize) size
- -(void) SizeToFit
- -(void) layoutsubviews
- -(void) layoutifneeded
- -(void) setneedslayout
- -(void) Setneedsdisplay
- -(void) DrawRect
The layoutsubviews is called in the following cases:
1. Init initialization does not trigger Layoutsubviews
When initialized with initWithFrame, however, when the value of rect is not cgrectzero, it also fires
2, Addsubview will trigger Layoutsubviews
3, set the view frame will trigger the layoutsubviews, of course, if the frame value has changed before and after the setting
4, rolling a uiscrollview will trigger layoutsubviews
5. Rotate screen will trigger the Layoutsubviews event on the parent UIView
6, change the size of a uiview will also trigger the parent UIView on the Layoutsubviews event
In Apple's official documentation, the following highlights:
You should override this method only if the autoresizing behaviors of the subviews does not offer the behavior you want.
Layoutsubviews, we need to call when we are adjusting the child view position inside a class.
In turn, this means: If you want to set the Subviews location externally, do not rewrite it.
Refresh child Object Layout
-layoutsubviews method: This method, the default does not do anything, need to rewrite the subclass
-setneedslayout method: Marked for the need to re-layout, asynchronous call layoutifneeded refresh the layout, not immediately refreshed, but layoutsubviews must be called
-layoutifneeded method: If there are tags that need to be refreshed, call layoutsubviews immediately to lay out (without a tag, layoutsubviews will not be called)
If you want to refresh immediately, call [view Setneedslayout], set the tag to need layout, and immediately call [view layoutifneeded] to implement the layout
Before the view is first displayed, the tag always "needs to be refreshed" and can be called directly [view layoutifneeded].
IOS Layoutsubviews Summary