Tuesday, 18 June 2013

Force start iOS application in Landscape Mode OR Portrait Mode.


If you want your application to be in Landscape mode.

STEP 1: Click on "MyProject.xcodeproj" file 
STEP 2: Select the TARGET Goto Project summary.
STEP 3: Choose supported interface orientations as landscape.



#import <objc/message.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Force start in Landscape Mode
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];


 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight);
        }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSLog(@"orientation:%d",[[UIDevice currentDevice]orientation]);

    return YES;
}


You can use the steps for portrait mode, just change the orientation to portrait mode in supported orientation. And in STEP 3: 
[[UIApplication sharedApplicationsetStatusBarOrientation: UIInterfaceOrientationPortraitRight];



If you support all the orientations, then you need to update your configurations in 
Info.p-list.

Either
Using Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</plist>

OR

Just add "Supported interface orientations" and values in the array starting with
For Landscape:
  "UIInterfaceOrientationLandscapeRight" or "UIInterfaceOrientationLandscapeLeft"
For Portrait: 
   "UIInterfaceOrientationPortrait" or "UIInterfaceOrientationPortraitUpsideDown"

No comments:

Post a Comment