UIViewController loads rotated to landscape but only supports portrait
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.
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.
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
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];
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]
.
精彩评论