IOS Layoutsubviews Summary

Source: Internet
Author: User

iOS layout mechanism related methods

    1. -(Cgsize) Sizethatfits: (cgsize) size
    2. -(void) SizeToFit
    1. -(void) layoutsubviews
    2. -(void) layoutifneeded
    3. -(void) setneedslayout
    1. -(void) Setneedsdisplay
    2. -(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

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.