The code should be such
Appdelegate.h
@property () BOOL restrictRotation; -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;
AppDelegate.m
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if(self.restrictRotation) return UIInterfaceOrientationMaskLandscape; //you can change upsidedown etc else return UIInterfaceOrientationMaskPortrait; // you can change landscape right or left only //this should be your initial app orientation because by default value of bool is false }
SettingsViewController (where do you want to change the orientation of the entire application)
-(void) restrictRotation:(BOOL) restriction { AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; appDelegate.restrictRotation = restriction; [appDelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window]; }
don't forget to import appdelegate.h;)
And finally, from your switch to change orientation,
[self restrictRotation:YES];
You can manipulate supportedInterfaceOrientationsForWindow as required, for example, you can set the property as int instead of bool, and you can set multiple orientation for different int values.
hope this helps :)
source share