in my app i have a navigation controller that shows a view controller forced to portrait orientation. On tap on a button on screen a push is performed and a view controller is shown, and it doesn't have restrictions about orientation, it support all orientations. The problem is that when i tap on back button and i am in landscape, the first view controller is shown in landscape, and not in portrait has expected. I've implemented all method for orientation, such as supportedInterfaceOrientation for each view controller, but i don't found a way to force the first view controller to portrait.
Asked
Active
Viewed 640 times
0
-
https://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8?rq=1 – saurabh Jun 06 '17 at 16:48
-
1Possible duplicate of [How to force view controller orientation in iOS 8?](https://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8) – dandan78 Nov 24 '17 at 14:12
1 Answers
1
I was able to force orientation for a particular view by calling the following in the viewWillAppear
event:
// LOCK SCREEN ORIENTATION TO LANDSCAPE
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setLockLandscape:YES];
// CHECK CURRNET ORIENTATION
if([[UIDevice currentDevice] orientation] != UIInterfaceOrientationLandscapeRight){
// TRANSITION ORIENTATION TO LANDSCAPE
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}else{
// SET ORIENTATION TO PORTRAIT
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
// TRANSITION ORIENTATION TO LANDSCAPE
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
Transition The View
This will animate the view to a particular orientation, however the orientation will not be locked necessarily:
// TRANSITION ORIENTATION TO LANDSCAPE
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
Lock The Orientation
This will lock the view:
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setLockLandscape:YES];
I was able to call [appDelegate setLockLandscape:YES];
from the property lockLandscape
I created in my appDelegates
as well as the following function:
@property (assign, nonatomic) BOOL lockLandscape;
//////////////////////////////////////////
// DELAGATE FUNCTION - LOCK ORIENTATION
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
// CHECK LOCK LANDSCAPE
if (self.lockLandscape){
// LOCK LANDSCAPE
return UIInterfaceOrientationMaskLandscape;
}else{
// LOCK PORTRAIT
return UIInterfaceOrientationMaskPortrait;
}
} // END - FUNCTION DELAGATE
The other code was to fix a bug that occurred when transition from another type of landscape than what I was forcing.

Tony
- 2,890
- 1
- 24
- 35
-
I've tryed the [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; code, but the problem is that the pop animation of the navigation controller looks very bad when the view rotate to the selected orientation, so i was looking for a solution with a better graphic result – blackstar26 Jun 07 '17 at 07:46
-
@blackstar26 in my case I removed the navigation before any rotation. Is it possible you could maybe fade it in and out before and after the transition? – Tony Jun 07 '17 at 14:41