The Layoutsubviews of all evils

Source: Internet
Author: User

#万恶的LayoutSubviews

This article mainly introduces oneself in the project to encounter regarding the layoutsubviews related question, mainly stems from in gives the restriction the animation time, Viewcontroller's viewwilllayoutsubviews, Viewdidlayoutsubviews, and the view of the layoutsubviews will frequently carry out problems, if the Layoutsubviews method of improper handling, and animation frequently executed, will cause the interface card death, therefore, to deal with the special cautious.

Here is a brief introduction to the cause of the problem:

First, the life cycle of Viewcontroller should be familiar to all, and the loading process is probably like this:

2016-06-26 20:21:28.791 layoutsubviewstest[8653:2812815]-[viewcontroller viewwillappear:]
2016-06-26 20:21:28.798 layoutsubviewstest[8653:2812815]-[viewcontroller viewwilllayoutsubviews]
2016-06-26 20:21:28.798 LAYOUTSUBVIEWSTEST[8653:2812815]-[viewcontroller viewdidlayoutsubviews]
2016-06-26 20:21:28.800 LAYOUTSUBVIEWSTEST[8653:2812815]-[viewcontroller viewwilllayoutsubviews]
2016-06-26 20:21:28.800 LAYOUTSUBVIEWSTEST[8653:2812815]-[viewcontroller viewdidlayoutsubviews]
2016-06-26 20:21:28.802 LAYOUTSUBVIEWSTEST[8653:2812815]-[viewcontroller viewdidappear:]

In fact, we can find that Viewwilllayoutsubviews and Viewdidlayoutsubviews will perform frequently, but there is a situation Viewwilllayoutsubviews and Viewdidlayoutsubviews also execute, and that's when we do constraint animation, like this:

    [UIView animatewithduration:0.3 animations:^{
        self.topConstraint.constant-=;
    }];

Look at the implementation process:

2016-06-26 20:25:38.974 layoutsubviewstest[8666:2814946]-[viewcontroller viewwilllayoutsubviews]
2016-06-26 20:25:38.975 layoutsubviewstest[8666:2814946]-[viewcontroller viewdidlayoutsubviews]

We didn't do it once, and the two methods were executed once, mainly because the AutoLayout mechanism would rearrange the interface after the constraint animation was still in line, so the two methods would be executed again, so it is recommended that you do not deal with complex problems in both methods. In particular, when dealing with animations that vary according to gestures, these two methods are particularly frequent, especially with regard to layer-related processing, and not to be executed in these two methods.

Here, I believe you are very clear about the problem, I hope you avoid such a mistake, otherwise it is difficult to find the reason.

In fact, there is also a method of frequent implementation, that is the view of the Layoutsubviews, if the interface between the animation, there are some manually added to the child view, then you need to pay special attention, because we often just focus on the Viewcontroller related methods, Easy to ignore the relevant methods of view, when the constraint animation execution process, view layoutsubviews will also be frequently executed, you can write demo test.

Before is encountered such a problem, that is in layoutsubviews processing layer related processing, such as Shadow, corner and other similar processing, in the gesture animation, once the operation is too frequent, the interface will die, the cause of the problem is also looking for a long time, It is recommended that you use the Xcode instruments to find performance-related issues, because you can see the specific memory-consuming methods that are easy to use.

In short, I hope I stepped on the pit, for everyone to help in the development. We recommend that you do not in the lauoutsubviews related methods, to deal with the problem of special memory consumption, you can get the right frame of view, do not deal with too many things.

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.