2
votes

I have an iPhone app that supports all device orientations, but I have a specific modal view that I only want to display in landscape.

If the device is in landscape orientation it works fine, but if the device is physically held in portrait orientation it does not display the way I would like.

Because the device is held in a portrait orientation the image is cropped and the left and right sides are off the edges of the display. Since the image is only as high as the narrow dimension, the top and bottom shows the background of the parent view. If I then rotate the device into a landscape orientation everything is then fine.

In my research I frequently come across shouldAutorotateToInterfaceOrientation, but I believe that is an event that only gets fired when the device is physically rotated which does not apply to my situation. The device is physically held in portrait orientation but I want to display the view as if it was held in landscape.

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight] is also mentioned but I have never seen that have any effect when I've tried using it.

So I guess my question is if there is a simple way to force the initial display of a view in an orientation different than the physical orientation of the device and exactly how to do it.

I am using the following logic to modally display my view:

    TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]];
    titleController.delegate = self;
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:titleController animated:YES];
    [titleController release];

I want to always display the modal view in a landscape orientation, even when the device is held in portrait.

UPDATE

Found a solution that works:

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }

However, when dealing with the orientation UIInterfaceOrientationPortraitUpsideDown, it initially displays the view with the proper orientation but then shifts it back to a portrait orientation with clipped sides and shows the background above and below the image.

I have tried tweaking the angle to 270, -90, and other values but always reverts to portrait when in the UIInterfaceOrientationPortraitUpsideDown orientation.

Why?

3

3 Answers

2
votes

This approach did everything I needed:

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }

    TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]];
    titleController.delegate = self;
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:titleController animated:YES];
    [titleController release];

Rotate the orientation before presenting my modal view.

And I just removed support for UIInterfaceOrientationPortraitUpsideDown since that orientation wasn't needed.

1
votes

Have you tried :

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

And when you about to dismiss it change to the Portrait

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
0
votes

Solution 1: To change the startup orientation from the initial Portrait mode, you need to edit your info.plist file. Add an entry for "intial interface orientation" and set the value that you want.

Here is a screenshot of where to find the info.plist file. (if the link doesn't work, please leave a comment)


Solution 2: You can also try this in your navigation controller as a subview to the window and orientation notifications are only sent to the first subview in "window".

//.m file
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Hide status bar
    application.statusBarHidden = YES;
        // --- Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [viewController.view addSubview:navigationController.view];
    [window makeKeyAndVisible];
    return YES;
}