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