【问题标题】:Force portrait mode强制人像模式
【发布时间】:2013-08-27 19:18:42
【问题描述】:

好的,由于没有人回答我之前的问题,我开始相信可能没有简单的方法可以做到这一点。但我很乐观。这是我的问题:

在我的应用程序中,我使用常规 UIButton 从 ViewControllerOne 切换到 ViewControllerTwo。 ViewControllerOne 始终处于横向模式。 ViewControllerTwo 应该始终处于纵向模式。但是,当我在横向 ViewControllerOne 中按下按钮时,ViewControllerTwo 也处于横向模式,尽管我希望它切换到纵向模式,而不管按下按钮时用户如何旋转设备。

我将以下代码添加到我的 AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}

我将它添加到我的 ViewController 中,它应该只处于纵向模式:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;

}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

-(void)viewWillAppear:(BOOL)animated
{
    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationPortrait;
}

有没有办法告诉 viewController 进入纵向模式,即使之前的视图是横向的?也许我可以创建一个自定义 segue,在按下按钮时强制视图处于纵向模式?这里最好/官方的解决方案是什么?

【问题讨论】:

标签: iphone ios objective-c orientation


【解决方案1】:

根据我对问题的理解,我假设您希望第二个视图控制器是纵向的,而第一个视图控制器是横向的。

对于第二个viewcontroller,添加这个方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

对于第一个视图控制器:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscape);

}

【讨论】:

    【解决方案2】:

    检查问题How to handle different orientations in iOS 6. 查看那里的答案以获取您所需要的项目示例。

    基本上,您需要在视图控制器(您要旋转的那个)中嵌入一个自定义导航控制器。在此自定义导航控制器中添加以下方法(用于横向,但您可以将其替换为纵向)

    - (NSUInteger)supportedInterfaceOrientations
    {
        return self.topViewController.supportedInterfaceOrientations;
    }
    

    并添加到应该旋转的视图控制器:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    

    确保在您的项目中启用纵向、横向右侧和横向左侧方向。然后,如果您想阻止特定视图的某些方向:

    – application:supportedInterfaceOrientationsForWindow:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多