Childviewcontroller Frame exception problem when Viewcontroller nested in IOS AutoLayout case

Source: Internet
Author: User


In recent projects, with storyboard, AutoLayout development, a viewcontroller nested multiple sub-viewcontroller, resulting in a coordinate exception problem when adding it to the parent Viewcontroller; tracking Code Discovery , this is due to the AutoLayout state, get frame data is inaccurate (or the timing is not right) caused by, on the internet for a long time, learn from each other, get the solution, the specific following:


Analysis:

AutoLayout state, after the execution of Viewdidload, Viewwillappear and other methods, will also execute the Viewdidlayoutsubviews method, and the key to solve the problem is here.

In this method, we can re-frame the view of a sub-view or even a childviewcontroller.


The sample code is as follows:

-(void) viewdidlayoutsubviews{    [Super viewdidlayoutsubviews];        Self.contentScrollView.contentSize = Cgsizemake (Cgrectgetwidth (self.contentScrollView.frame) * 3, Cgrectgetheight ( Self.contentScrollView.frame));        if (IOS8) {                CGFloat subview_w = Cgrectgetwidth (self.contentScrollView.frame);                for (int i = 0; i < 3; i++) {                        UIView *subview = [Self.view Viewwithtag:subvc_viewtag + i];                        CGRect subviewframe = subview.frame;            subviewframe.origin.x = Subview_w * i;            Subview.frame = Subviewframe;        }    }
    IOS7 must execute    [Self.view layoutsubviews];}

Note: In this method, if iOS7, you must perform the

[Self.view Layoutsubviews];


Childviewcontroller Frame exception problem when Viewcontroller nested in IOS AutoLayout case

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.