When the iOS device is flat, frame and bounds are respectively

Source: Internet
Author: User

Project has two viewcontrollers, of which Viewcontroller is the root view controller, the background is red, there is a button, click on the load Greenviewcontroller, and display its view, the background is green.

The first is the Viewcontroller code:

#import "ViewController.h" #import "GreenViewController.h" @interface Viewcontroller () @end @implementation        Viewcontroller-(void) viewdidload {[Super viewdidload];    Self.view.backgroundColor = [Uicolor Redcolor]; Self.view.autoresizingMask = Uiviewautoresizingflexibleheight |        Uiviewautoresizingflexiblewidth;    UIButton *showgreenviewbtn = [UIButton buttonwithtype:uibuttontypecustom];    [Showgreenviewbtn settitle:@ "Show Green" forstate:uicontrolstatenormal];    Showgreenviewbtn.frame = CGRectMake (0, 0, 100, 44);    Showgreenviewbtn.center = Self.view.center; Showgreenviewbtn.autoresizingmask = Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin | Uiviewautoresizingflexibletopmargin |    Uiviewautoresizingflexiblebottommargin;    [Showgreenviewbtn addtarget:self Action: @selector (Showgreenview:) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:showgreenviewbtn];} -(void) Showgreenview: (ID) Sender {greenviewcontrOller *GREENVC = [Greenviewcontroller new]; [GREENVC show];} @end


Then there is the code for Greenviewcontroller:

#import "GreenViewController.h" #import "AppDelegate.h" @interface Greenviewcontroller () @end @implementation        greenviewcontroller-(void) viewdidload {[Super viewdidload]; Self.view.autoresizingMask = Uiviewautoresizingflexiblewidth |    Uiviewautoresizingflexibleheight; Self.view.backgroundColor = [Uicolor greencolor];}    -(void) show {appdelegate *appdelegate = [[UIApplication sharedapplication] delegate];    Uiviewcontroller *rootviewcontroller = AppDelegate.window.rootViewController;    [Rootviewcontroller addchildviewcontroller:self];        [Rootviewcontroller.view AddSubview:self.view];    NSLog (@ "Rootviewcontroller");    NSLog (@ "F%@", Nsstringfromcgrect (RootViewController.view.frame));        NSLog (@ "b%@", Nsstringfromcgrect (RootViewController.view.bounds));    NSLog (@ "Greenviewcontroller");    NSLog (@ "F%@", Nsstringfromcgrect (Self.view.frame));        NSLog (@ "b%@", Nsstringfromcgrect (Self.view.bounds)); [Self didmovetoparentviewcontroller:rootviewcontroller];} @end 


If the emulator is running, the view position is completely normal, so it must be running (the simulator pits the dead). Horizontal device, let the red view rotate. Click the Show Green button and the results are as follows:






All kinds of wonderful ...

Look at the console output:

2014-07-18 10:29:42.754 frameboundsrotate[8588:60b] rootviewcontroller2014-07-18 10:29:42.756 FrameBoundsRotate[ 8588:60B] F {{0, 0}, {568}}2014-07-18, 10:29:42.757 frameboundsrotate[8588:60b] B {{0, 0}, {568, 320}}2014-07-18 10:29 : 42.758 frameboundsrotate[8588:60b] greenviewcontroller2014-07-18 10:29:42.759 frameboundsrotate[8588:60b] F {{0, 0}, {320, 568}} 2014-07-18 10:29:42.760 frameboundsrotate[8588:60b] B {{0, 0}, {320, 568}}


The frame of the Rootviewcontroller view is still (0, 0, 320, 568) when the device is flat, and bounds becomes (0, 0, 568, 320). Greenviewcontroller view of the frame and bounds have not changed, because the Rootviewcontroller view of the frame has not changed, so Greenviewcontroller The Autoresizingmask property of the view does not work.

In order to solve the position distortion problem when the view is added after the above horizontal screen, add self.view.frame = RootViewController.view.boundsin the Show method, as follows:

-(void) show {    appdelegate *appdelegate = [[UIApplication sharedapplication] delegate];    Uiviewcontroller *rootviewcontroller = AppDelegate.window.rootViewController;    [Rootviewcontroller addchildviewcontroller:self];    [Rootviewcontroller.view AddSubview:self.view];    Self.view.frame = rootViewController.view.bounds;        NSLog (@ "Rootviewcontroller");    NSLog (@ "F%@", Nsstringfromcgrect (RootViewController.view.frame));    NSLog (@ "b%@", Nsstringfromcgrect (RootViewController.view.bounds));        NSLog (@ "Greenviewcontroller");    NSLog (@ "F%@", Nsstringfromcgrect (Self.view.frame));    NSLog (@ "b%@", Nsstringfromcgrect (Self.view.bounds));        [Self didmovetoparentviewcontroller:rootviewcontroller];}


Run again, no problem:



Console output:

2014-07-18 10:32:30.320 frameboundsrotate[8593:60b] rootviewcontroller2014-07-18 10:32:30.323 FrameBoundsRotate[ 8593:60B] F {{0, 0}, {568}}2014-07-18, 10:32:30.324 frameboundsrotate[8593:60b] B {{0, 0}, {568, 320}}2014-07-18 10:32 : 30.325 frameboundsrotate[8593:60b] greenviewcontroller2014-07-18 10:32:30.326 frameboundsrotate[8593:60b] F {{0, 0}, {568, 320}} 2014-07-18 10:32:30.327 frameboundsrotate[8593:60b] B {{0, 0}, {568, 320}}


Summarize:

Simulator Pit dead, remember the real machine debugging.


Demo Address: Click to open the link


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.