1, shielding appdelegate The following screen rotation method
#pragma mark-screen rotation //-(uiinterfaceorientationmask) Application: (uiapplication*) application Supportedinterfaceorientationsforwindow: (uiwindow*) window//{// return uiinterfaceorientationmaskportrait; // }
2, to Uinavigationcontroller and Uitabbarcontroller write two extension categories, this thing does not need to be referenced in the specific Viewcontroller
Uinavigationcontroller+autorotate.h
// // uinavigationcontroller+autorotate.h// fitmiss / /// Created by Bill on 15/12/16. // copyright©2015 Year Lear. All rights reserved. // #import <UIKit/UIKit.h>@interface uinavigationcontroller (autorotate) @end
Uinavigationcontroller+autorotate.m
////UINAVIGATIONCONTROLLER+AUTOROTATE.M//Fitmiss////Created by Bill on 15/12/16.//copyright©2015 Year Lear. All rights reserved.//#import "uinavigationcontroller+autorotate.h"@implementationUinavigationcontroller (autorotate)-(BOOL) shouldautorotate{return[Self.topviewcontroller shouldautorotate];}-(Nsuinteger) supportedinterfaceorientations{return[Self.topviewcontroller supportedinterfaceorientations];}-(uiinterfaceorientation) preferredinterfaceorientationforpresentation{return[Self.topviewcontroller preferredinterfaceorientationforpresentation];}@end
Uitabbarcontroller+autorotate.h
// // uitabbarcontroller+autorotate.h// fitmiss / // / Created by Bill on 15/12/16. // copyright©2015 Year Lear. All rights reserved. // #import <UIKit/UIKit.h>@interface uitabbarcontroller (autorotate)@end
Uitabbarcontroller+autorotate.m
////UITABBARCONTROLLER+AUTOROTATE.M//Fitmiss////Created by Bill on 15/12/16.//copyright©2015 Year Lear. All rights reserved.//#import "uitabbarcontroller+autorotate.h"@implementationUitabbarcontroller (autorotate)-(BOOL) shouldautorotate{return[Self.selectedviewcontroller shouldautorotate];}-(Nsuinteger) supportedinterfaceorientations{return[Self.selectedviewcontroller supportedinterfaceorientations];}-(uiinterfaceorientation) preferredinterfaceorientationforpresentation{return[Self.selectedviewcontroller preferredinterfaceorientationforpresentation];}@end
3. In the Viewcontroller used in the re-rewrite these three methods, you can override the following method in the root Viewcontroller, you can implement the vertical screen
- (BOOL) shouldautorotate{ return NO;
From the online user's reply content, as follows:
The following methods are only valid for projects where the deploy target is greater than or equal to iOS6, if the application of the master needs to support IOS5 (silence), pass.
- Set the direction in the Info.plist, including all the directions you need, in the title, Upsidedown and Landscapeleft;
- Inherit Uitabbarcontroller,override the following three methods
- (BOOL)shouldAutorotate{ return [self.selectedViewController shouldAutorotate];} - (NSUInteger)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations];} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.selectedViewController preferredInterfaceOrientationForPresentation];}
- Inherit the same method as in Uinavigationcontroller,override and Uitabbarcontroller, and change the Selectedviewcontroller to Topviewcontroller
- (BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate];} - (NSUInteger)supportedInterfaceOrientations{ return [self.topViewController supportedInterfaceOrientations];} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.topViewController preferredInterfaceOrientationForPresentation];}
- In the viewcontroller that really implements the interface, override the above three methods, the override rule is as follows:
Preferredinterfaceorientationforpresentation indicates the direction of Viewcontroller initial display;
Supportedinterfaceorientations is all the directions supported in the Viewcontroller;
Shouldautorotate indicates whether the spin screen is allowed.
Process description
First, for any one viewcontroller, iOS will be info.plist in the settings and the current Viewcontroller preferredinterfaceorientationforpresentation and supportedinterfaceorientations three supported methods to do a Intersection operations, if the intersections are not empty, the preferredinterfaceorientationforpresentation is the initial direction, and all directions in the intersection are supported, but only when Shouldautorotate returns YES. Allows rotation from the initial direction to another direction. If the intersection is empty, it is crash when entering Viewcontroller, and the error message indicates that the intersection is empty.
Second, Uinavigationcontroller is somewhat special, it is difficult to use the conventional API to achieve the same NAVIVC in the Viewcontroller in different directions between the freely switch. (If you go to such places such as search, will find a present empty viewcontroller and then dismiss it hacky trick, not recommended), if you want to switch between the screen, we recommend using the Presentxxx method.
Again, there is a delegate method in Appdelegate that can dynamically set the rotation direction that the application supports, and the return value of this delegate overrides the fixed setting in Info.plist. The convenience of using this method is self-evident, but the disadvantage is to understand which Viewcontroller is going to be displayed, which is likely to lead to increased coupling;
Finally, the above are the individual in the IOS8 SDK under the results of the practice, please the main combination of engineering practical reference use.
How iOS Implements screen rotation