最近在自己的项目里面
The implementation method is as follows:
1 first need to select the supported screen orientation in Xcode
2 in Appdelegate
. h
@property (nonatomic,assign)NSInteger allowRotate;
. m medium
//This method will be called when the device changes in the screen-(Nsuinteger) Application: (UIWindow *) window{//NSLog (@ "Direction =============%ld", _allowrotate); if (_allowrotate = 1) {return Uiinterfaceorientationmaskall; }else{return (uiinterfaceorientationmaskportrait);}} //returns whether device auto-rotation is supported-(bool) shouldautorotate{if (_allowrotate = = 1) {return yes; } return no;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
3 in a controller that needs to support the screen:
In Viewwillapplear
//在视图出现的时候,将allowRotate改为1, AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; delegate.allowRotate = 1;
In Viewwilldisappear
//在视图出现的时候,将allowRotate改为0, AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; delegate.allowRotate = 0;
After writing the above code, you will find some problems: when the horizontal screen page directly click the "Back" button to exit, the page is still a horizontal screen, and we need only one page can be horizontal screen, the test needs to add the following code in the Viewwilldisappear:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = UIInterfaceOrientationPortrait; [invocation setArgument:&val atIndex:2]; [invocation invoke]; }
At this point, you can make the app only have Settings page support screen!
In this case, if the app requires the user to change the UI in a horizontal vertical screen (a different UI for a horizontal screen and a vertical screen), you can do so in the following ways
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ // do something before rotation if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 屏幕从竖屏变为横屏时执行 }else{ 屏幕从横屏变为竖屏时执行 } }- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ // do something after rotation}
IOS enables a single page to support vertical and horizontal screen, other pages can only portrait screen