2

Is there a way to load a portrait only view controller in portrait orientation even though the parent view controller (and phone) is in landscape orientation?

This particular UIViewController only fits in Portrait mode (it's a color picker), so the code in shouldAutorotateToInterfaceOrientation: returns YES only for portrait orientations. That works fine if you turn the phone while the view is already loaded, it stays in portrait, but it doesn't help if the phone is already in landscape when the view's NIB is loaded.

When the screen is already in landscape orientation when the ViewController is loaded, the view is in portrait layout and the bottom half of the view is cutoff. I tried coming up with a landscape layout but the segmented control is a problem.

EDIT - I've tried using setStatusBarOrientation in viewWillAppear but it only affects the status bar, the navigation bar and view are still rotated for landscape.

This happens with 3.1.3 and 4.0 on the iPhone.

futureelite7
  • 11,462
  • 10
  • 53
  • 87
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • 2
    I'm not the only person with this problem and none of the previous questions (like [here](http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation/1850462#1850462) and [here](http://stackoverflow.com/questions/2093821/viewcontroller-has-wrong-orientation-after-landscape-only-has-been-popped)) have useful answers. [This one](http://stackoverflow.com/questions/2144520/iphone-allow-landscape-orientation-on-just-one-viewcontroller) says it can't be done. Is this just a limitation we have to live with? – progrmr Jun 22 '10 at 18:55

4 Answers4

6

If it works for your app, you could consider using a modal view controller instead. IIRC presenting a modal view controller does handle rotating properly.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • Bingo!! That works, it gets oriented and laid out in portrait. There's no nav bar but that's easily dealt with. – progrmr Jun 26 '10 at 04:22
2

In this example I want the view to start in landscape mode, even if it was pushed from portrait mode.


@implementation MyView
-(void)layoutSubviews {
    // Set subview frames here
    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationUnknown:
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
            self.transform = CGAffineTransformMakeRotation(M_PI/2.0);
            self.transform = CGAffineTransformTranslate(self.transform, -80.0f, 80.0f);
            break;
        case UIDeviceOrientationLandscapeLeft:
        case UIDeviceOrientationLandscapeRight:
            self.transform = CGAffineTransformIdentity;
            break;
    }
}
@end

@implementation MyController -(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration { [self.view setNeedsLayout]; } @end

cyborch
  • 31
  • 1
1

Under normal circumstances, when displaying a new UIViewController it should automatically rotate to portrait if it only supports portrait and the app is currently in landscape. This only applies to newer versions of the OS (but I assume you're probably running on 3.1.3?).

In any case, to force the app into portrait:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
Ed Marty
  • 39,590
  • 19
  • 103
  • 156
  • I tried that but it doesn't work. It moves the status bar, but the navigation bar is still in the wrong place and the view is still rotated. – progrmr Jun 21 '10 at 18:30
  • How is the navigation controller displayed? Is it the root view controller initially added to the window? Or is it presented in some other way? Do all of the parent view controllers support rotating to portrait? And how is the color picker displayed on the navigation controller? Is it the root view or pushed on to an existing controller? – Ed Marty Jun 21 '10 at 19:14
  • The nav controller is the root VC added in the app delegate. All of the view controllers support portrait and landscape except this one. The color picker is displayed by calling pushViewController:animated: on the nav controller. The root and parent view controller are in landscape orientation at the time the color picker is pushed. – progrmr Jun 21 '10 at 20:46
0

I think that it is not possible to push a portrait view controller to navigation controller that is already in landscape mode.
Try to imagine its animation (the navigation bar)...

I would suggest you to add a landscape mode for your color picker.

Another solution might be to rotate the entire view when entering to the color picker in landscape mode.
You can take the current orientation by [[UIDevice currentDevice] orientation].

Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • You may be right, but if you push the UIImagePickerController (which only supports portrait) from a landscape view, it does animate properly and slide in the picker with navbar in the portrait orientation. Perhaps they are using internal APIs to accomplish this though or it might have something to do with the fact that the UIImagePickerController is also a UINavigationController. – progrmr Jun 25 '10 at 16:24