Layoutsubviews How to manually invoke Viewwilllayoutsubview

Source: Internet
Author: User

Http://www.07net01.com/program/2015/12/1017687.html

IOS AutoLayout Mechanisms related methods-(cgsize) Sizethatfits: (cgsize) Size-(void) SizeToFit

——————--(void) layoutsubviews-(void) layoutifneeded-(void) setneedslayout

—————— –-(void) Setneedsdisplay-(void) DrawRect

Layoutsubviews are invoked in the following situations:

1. Init initialization does not trigger Layoutsubviews

However, when initializing with initWithFrame, when the RECT value is not cgrectzero, it also triggers

2, Addsubview will trigger Layoutsubviews

3, set the view frame will trigger layoutsubviews, of course, the premise is the value of the frame set before and after the change

4, rolling a uiscrollview will trigger layoutsubviews

5, rotate screen will trigger the Layoutsubviews event on the parent UIView

6, change a uiview size will also trigger the parent UIView on the Layoutsubviews event

In Apple's official document, it stresses:

When the Autoresizing property doesn't reach what you want, you need to rewrite the method.

You are should override this method is only if the autoresizing behaviors of the subviews does not offer the behavior for you want.

Layoutsubviews, when we adjust the position of a child view within a class, we need to call it.

The converse means: If you want to set the subviews position externally, do not rewrite it.

Refresh child Object Layout

-layoutsubviews method: This method, default does not do anything, need subclasses to rewrite
-setneedslayout method: Mark to require a layout, asynchronous call layoutifneeded refresh layout, do not immediately refresh, but Layoutsubviews must be called
-layoutifneeded method: If there is a tag that needs to be refreshed, call layoutsubviews immediately to layout (if there are no tags, no call to Layoutsubviews)

If you want to refresh now, call [view Setneedslayout] First, set the tag to the layout you want, and then immediately call [view layoutifneeded] to implement the layout

The tag is always "need to refresh" before the view is first displayed, and you can call [view layoutifneeded] Directly

Re-painting

-drawrect: (CGRect) Rect method: Override this method to perform a redraw task
-setneedsdisplay method: Mark as needing redrawing, call DrawRect asynchronously
-setneedsdisplayinrect: (CGRect) Invalidrect method: marking as requiring partial redraw

SizeToFit will automatically invoke the Sizethatfits method;

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

Sizethatfits The parameter passed in is receiver the current size, returning a suitable size

SizeToFit can be called directly by hand

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

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

Layoutsubviews Subviews to a new layout

Layoutsubviews method calls precede the DrawRect

Setneedslayout is labeled with a receiver that needs to be laid out and automatically invoked in the next cycle of the system Runloop. Layoutsubviews

The Layoutifneeded method, like its name, Uikit determines whether the receiver requires layout. According to Apple's official documentation, the Layoutifneeded method should be this way

Layoutifneeded traversal is not a superview chain, it should be subviews chain

DrawRect is a redrawing of the receiver to get the context

Setneeddisplay receiver marked with a need to be redrawn in the next draw cycle automatically redraw, the IPhone device refresh rate is 60hz, which is 1/60 seconds after redrawing

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.