Explanation of the relationship between the Setneedslayout, layoutifneeded and Layoutsubviews methods of UIView

Source: Internet
Author: User

Layoutsubviews Summary

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]

Redraw

-drawrect: (CGRect) Rect method: Override this method to perform a redraw task
-setneedsdisplay method: Mark as required redraw, call DrawRect asynchronously
-setneedsdisplayinrect: (CGRect) Invalidrect method: Marked to require partial redraw

SizeToFit will automatically call the Sizethatfits method;

SizeToFit should not be overridden in subclasses and should be rewritten sizethatfits

Sizethatfits The parameter passed in is the current size of receiver and returns a suitable size

SizeToFit can be manually called directly

SizeToFit and Sizethatfits methods are not recursive, to subviews is not responsible, only responsible for their own

———————————-

Layoutsubviews re-layout of subviews

Layoutsubviews method invocation precedes DrawRect

Setneedslayout a tag that needs to be re-laid on the receiver label and is automatically called on the next cycle of the system Runloop layoutsubviews

Layoutifneeded the name of the method, Uikit will determine if the receiver needs layout. According to Apple's official documentation, the Layoutifneeded method should be

Layoutifneeded traversal is not the Superview chain, it should be the Subviews chain

DrawRect is a redraw of receiver that gets the context

Setneeddisplay a marker that needs to be redrawn on the receiver label, automatically redrawn on the next draw cycle, and iphone device refresh rate is 60hz, which is 1/60 seconds after redrawing

UIView Setneedslayout, the relationship between layoutifneeded and Layoutsubviews methods is explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.