The role and invocation mechanism of iOS development layoutsubviews

Source: Internet
Author: User

There is a method in UIView layoutSubviews , what is the specific function of this method?

- (void)layoutSubviews;    // override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.
Official Document explanation

Let's take a look at an explanation of Apple's official documentation:

The default implementation of this method does nothing on IOS 5.1 and earlier. Otherwise, the default implementation uses any constraints that has set to determine the size and position of any subviews . Subclasses can override this method as needed to perform more precise layout of their subviews. Should override this method only if the autoresizing and constraint-based behaviors of the subviews does not offer the B Ehavior you want. You can use your implementation to set the frame rectangles of your subviews directly. You should is not a call this method directly. If you want-to-force a layout update, call the Setneedslayout method instead to does so prior to the next drawing update. If you want to update the layout of your views immediately, call the Layoutifneeded method.

The last paragraph says, do not call this method directly. If you want to force the layout to be updated, you can call setNeedsLayout the method, and if you want to immediately count your views , you need to call the layoutIfNeeded method.

Layoutsubviews effect

layoutsubviews is to subviews re-layout. For example, when we want to update the location of the sub-view, we can call

layoutSubviewsThe default is not to do anything, when used, need to be rewritten in self-thunder.

Layoutsubviews The following conditions will be called

The official Apple document has emphasized that you cannot directly invoke layoutsubviews under what circumstances will it be called? Through Baidu Search, found the following several situations layoutsubviews will be called.

  1. called Directly setLayoutSubviews . (This is stated in the official Apple document above)
  2. addSubviewThe time.
  3. When view the frame change occurs.
  4. UIScrollViewwhen it's sliding.
  5. rotation screen will trigger the parent uiview " FONT-SIZE:0.85EM; Font-family:consolas,inconsolata,courier,monospace; margin:0px 0.15em; padding:0px 0.3em; White-space:pre-wrap; border:1px solid RGB (234,234,234); Background-color:rgb (248,248,248); Display:inline ">layoutsubviews event.
  6. Changing a UIView size also triggers the UIView event on the parent layoutSubviews .

I tested it briefly, and it's basically going to be called. Attention:

layoutSubviewsThe method is very convenient when laying out the self-thunder view. You can do it yourself and get a deeper understanding layoutSubviews of the invocation mechanism.

?

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The role and invocation mechanism of iOS development layoutsubviews

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.