Horizontal screen UIWindow Add multiple view auto-rotation solutions

Source: Internet
Author: User

1, do not directly in the UIWindow directly using the UIView, but load the root to try to control (Uiviewcontroller) and then use the controller to change UIView, which is Apple's official recommended way

[Self Beginappearancetransition:yes animated:no];    [Parentviewcontroller addchildviewcontroller:self];    [Parentviewcontroller.view AddSubview:self.view];    [Self didmovetoparentviewcontroller:self];    [Self endappearancetransition];

2.

In a horizontal screen app, use Keywindow to add a view, toggle the device landscape orientation, and discover that the added view does not change direction.

And only the rootviewcontroller of window can hear the change of the screen rotation.

[[Uiapplicationsharedapplication].keywindow Addsubview:view];

Here's how to fix it:

Define Rotatewindow inheritance UIWindow, and override the Addsubview method. The window in Appdelegate uses Rotatewindow.

#define Degreestoradian (x) (M_PI * (x)/180.0)-(void) Addsubview: (UIView *) View {      If1) {        float angle = ([uidevice currentdevice].orientation = = Uideviceorientationlandscapeleft)? *:--;         = cgaffinetransformmakerotation (Degreestoradian (angle));         0 0 );    }    [Super Addsubview:view];}

Define Rotateviewcontroller as the rootviewcontroller of the window and override the following methods

//horizontal screen only allowed-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation {//Return YES for supported orientations.    return(interfaceorientation = = Uiinterfaceorientationlandscapeleft | | interfaceorientation = =uiinterfaceorientationlandscaperight);}//Modify Add view orientation when device is rotated- (void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (NSTimeInterval) Duration {UIWindow*window =[UIApplication Sharedapplication].keywindow; inti =0;  for(UIView *viewinchwindow.subviews) {if(I! =0) {Cgaffinetransform transform=View.transform; Transform= Cgaffinetransformrotate (Transform, Degreestoradian ( the)); View.transform=transform; } I++; }}3, Methods:-(void) Rotateview {UIWindow*keywindow =[[UIApplication sharedapplication] Keywindow]; UIView*bgview =[[UIView alloc] initWithFrame:keywindow.bounds]; [Keywindow Addsubview:bgview]; [Bgview release]; [Bgview Addsubview:_overlayview]; [Bgview addsubview:self]; Self.center= Cgpointmake (keywindow.bounds.size.width/2.0f, Keywindow.bounds.size.height/2.0f); //Rotate the image by the specific angle.[Bgview settransform:cgaffinetransformmakerotation (angle)]; [Self fadeIn]; } 

Originating From: http://blog.csdn.net/leikezhu1981/article/details/38072221

Horizontal screen UIWindow Add multiple view auto-rotation solutions

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.