blackstar26
blackstar26

Reputation: 146

Force orientation on view controller

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.

Upvotes: 0

Views: 648

Answers (1)

Tony
Tony

Reputation: 3068

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.

Upvotes: 1

Related Questions