UIViewController加载旋转到横向但仅支持纵向

时间:2010-06-21 16:56:56

标签: iphone uiviewcontroller orientation

即使父视图控制器(和手机)处于横向方向,有没有办法以纵向方式加载纵向视图控制器?

这个特殊的UIViewController只适合纵向模式(它是一个颜色选择器),因此shouldAutorotateToInterfaceOrientation中的代码:仅对纵向方向返回YES。如果您在视图已加载时转动手机,它可以正常工作,它会保持纵向状态,但如果在加载视图的NIB时手机已处于横向状态,则无效。

当加载ViewController时,屏幕已处于横向方向,视图采用纵向布局,视图的下半部分为截止。我尝试提出横向布局,但分段控制是一个问题。

编辑 - 我尝试在viewWillAppear中使用setStatusBarOrientation,但它只会影响状态栏,导航栏和视图仍会针​​对横向旋转。

iPhone上的3.1.3和4.0会发生这种情况。

4 个答案:

答案 0 :(得分:6)

如果它适用于您的应用,您可以考虑使用模态视图控制器。 IIRC呈现模态视图控制器可以正确处理旋转。

答案 1 :(得分:2)

在此示例中,我希望视图以横向模式启动,即使它是从纵向模式推送的。


@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

答案 2 :(得分:1)

在正常情况下,当显示新UIViewController时,如果它仅支持纵向且应用当前处于横向状态,它应自动旋转为纵向。这仅适用于较新版本的操作系统(但我认为你可能在3.1.3上运行?)。

在任何情况下,强迫应用程序成为肖像:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

答案 3 :(得分:0)

我认为无法将纵向视图控制器推送到已处于横向模式的导航控制器 试着想象它的动画(导航栏)......

我建议您为颜色选择器添加横向模式。

另一种解决方案可能是在横向模式下进入颜色选择器时旋转整个视图 您可以按[[UIDevice currentDevice] orientation]获取当前方向。

相关问题