It is an superclass file name Apporientationviewcontroller.h/m
. And i am calling this super class in all other subclasses. So that if "isPortraitModeONN" is "ON"
then all the screen should work only in portrait mode. if user try to changes the device to landscape it should not rotate. it should be always in portrait mode if switch is "ON".. In my case while laughing the app it is in portrait mode. but if i rotate the screen it is changing towards landscape. but it should not change its orientation until turning OFF the switch in settings..
if(isPortraitModeONN)
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
else
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft){
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
}
else{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
}
This code works for me somewhat..
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator: (id<UIViewControllerTransitionCoordinator>)coordinator
{
///if isPortraitMode On then force the orientation to portrait if it's other than Portrait
///else force the orientation to landscape
if (isPortraitModeONN)
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
// do whatever
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(orientation)) {
///App is rotating to landscape, force it to portrait
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
}
else{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
// do whatever
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation)) {
///App is rotating to portrait, force it to landscape
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
}
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
You can do like this-
@property () BOOL isPortraitModeONN;
in AppDelegate.h
class
.Now code this in AppDelegate.m
-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.isPortraitModeONN=YES;
return YES;
}
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.isPortraitModeONN)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscape;
}
Now Add IBAction
of UIButton
for changing orientation in Apporientationviewcontroller.m
--
- (IBAction)changeOrientation:(id)sender {
if (allowedToRotate) {
allowedToRotate=NO;
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.isPortraitModeONN = NO;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}else{
allowedToRotate=YES;
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.isPortraitModeONN = YES;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
}
define in Apporientationviewcontroller.m
BOOL allowedToRotate;
default value is YES
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With