0
votes

I am having trouble starting the app in landscape mode. The view is always shown in portrait mode.

I have the following configured in the plist:

<key>UISupportedInterfaceOrientations</key>
<array>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
</array>

The UIViewController is created in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myViewController = [MyViewController alloc];

    UIView *myView = [[UIView alloc] init];
    myView = [UIColor whiteColor];
    self.myViewController.view = myView;

    self.window.rootViewController = self.myViewController;
    self.window makeKeyAndVisible];
    return YES;
}

Added shouldAutorotateToInterfaceOrientation to MyViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

When the device is in portrait mode and the app starts, the status bar is in landscape mode. However when the UIView gets shown, it will end up in portrait mode. The same thing happens when the device is in landscape mode.

However, when I create the UIViewController in Interface Builder, it works.

Is this related to autoresizemasking because it didn't work when I add this in AppDelegate.m:

 myView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
3
are you developing for iOS 6 ??Mutawe
Working on latest Xcode 4.5 and iOS 5.0Nora Olsen
Looks like I forgot to init the UIViewController. It worked after that.Nora Olsen

3 Answers

0
votes

Replace this code in your ViewController :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
} 
0
votes

Use this to find orientation at startup..it will work correct:

UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
if (! UIDeviceOrientationIsValidInterfaceOrientation(orientation))
    orientation = [UIApplication sharedApplication].statusBarOrientation;

Then use this orientation to rotate your view

0
votes

In your code I found a syntax error, I think you mean myView.backgroundColor = [UIColor whiteColor]; But this is the only mistake, your code works fine, the app starts and stay in Landscape mode, with or without myView.autoresizingMask

My environment: xCode 4.5 device iPhone 4S with IOS 6.0