强制一个视图控制器ios的景观

时间:2013-08-23 18:16:52

标签: iphone ios uinavigationcontroller orientation viewcontroller

我已经看过类似问题的几个答案,但答案都没有。我有一个应用程序,我需要一切portait除了我有一个照片查看器。在目标菜单的支持方向部分,我只有肖像。如何强制我的一个视图成为风景。它正在从导航控制器推入堆栈,但我正在使用故事板来控制所有这些。

3 个答案:

答案 0 :(得分:14)

由于答案似乎隐藏在问题的评论中,并且由于ArunMak的答案非常混乱,我只会提供我发现的内容:

我所要做的就是将这个函数添加到视图的自定义UIViewController子类中:

- (NSUInteger)supportedInterfaceOrientations {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        // iPad: Allow all orientations
        return UIInterfaceOrientationMaskAll;
    } else {
        // iPhone: Allow only landscape
        return UIInterfaceOrientationMaskLandscape;
    }
}

请注意,项目需要允许所有方向(即:纵向,横向左侧,横向右侧 - 但不要在iPhone上颠倒!)。

如果要将某些或大多数视图限制为Portrait,则需要在每个视图控制器中实现上述方法(或者为它使用公共超类,并从中继承所有其他视图) - 如果限制设备Info.plist中的方向只是肖像,应用程序甚至不会想到进入风景。

答案 1 :(得分:2)

是的,可以使用此代码:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return UIInterfaceOrientationMaskLandscape;
    }

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
        return orientation==UIInterfaceOrientationMaskLandscape;
    }

OR

在您的app delegate中尝试此方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (sglobalorientation isEqualToString:@"AllOrientation"]) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

在移动到该Landscape视图控制器之前,您需要将变量值sglobalorientation更改为该字符串值AllOrientation

并在您的横向视图控制器中,在您的视图中使用此代码将出现

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    DigitalSignatureViewController *digisign = [[DigitalSignatureViewController alloc]init];
    [self presentModalViewController:digisign animated:NO];
    [self dismissModalViewControllerAnimated:NO];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

再次移动到下一个视图控制器时,更改sglobalorientation字符串值并在下一个视图控制器中执行相同步骤。

答案 2 :(得分:0)

让我们试试这段代码:

    [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
self.navigationController.view.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), [UIScreen mainScreen].bounds.size.height/2);
CGFloat angle = 90 * M_PI / 180;
self.navigationController.view.transform = CGAffineTransformMakeRotation(angle);
self.navigationController.view.bounds = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height , [UIScreen mainScreen].bounds.size.width);