【问题标题】:IOS view controller orientationIOS视图控制器方向
【发布时间】:2013-01-19 09:50:07
【问题描述】:

我有父视图控制器和另一个模态视图控制器。 我在父级上下文中展示了模态视图控制器:

readingViewController * reading_view_controller = [[readingViewController alloc] initWithNibName:@"readingViewController" bundle:nil];
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentModalViewController:reading_view_controller animated:YES];

父视图控制器不会定位横向;所以,我在父 viewController 中添加了这些方法:

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortraitUpsideDown|UIInterfaceOrientationMaskPortrait;
}


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

呈现的视图控制器(模态呈现)应该面向所有可能的方向;所以,我添加了这些方法:

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

    return YES;
}

但是,因为我假设 (UIModalPresentationCurrentContext),呈现的 viewController 在 IOS 6.0 中没有定向,而它在 IOS 5.0 中按预期工作

请问这个问题怎么解决?

【问题讨论】:

    标签: ios uiviewcontroller presentmodalviewcontroller device-orientation


    【解决方案1】:

    如果你有一个基于标签的应用程序,那么首先在应用程序委托中添加一些代码以及self.window.rootviewcontroller-self.tabbarcontroller

            @implementation UITabBarController (SK8FASTR2App)
            -(BOOL)shouldAutorotate
            {
                return YES;
            }
    
            - (NSUInteger)supportedInterfaceOrientations
            {
                // your custom logic for rotation of selected tab
                 if (self.selectedIndex==3){
                 return UIInterfaceOrientationMaskPortrait;
                 }
                 else {
                UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown|UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight;
                 return UIInterfaceOrientationMaskAll;
                 }
    
            }
    
            @end
    
    
    
            before
    
            @implementation AppDelegate
    
    
        - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
                return  UIInterfaceOrientationMaskAll;
        }
    

    更改该类的方向

    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationLandscapeRight;
    }
    

    【讨论】:

      【解决方案2】:

      你应该实现

      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
      

      例如:-

      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          return UIInterfaceOrientationMaskAll;
      }
      

      【讨论】:

      • 我实现了,但是没有变化
      • 你的viewController在其他Controller下吗?我的意思是像 navigationController 或 tabViewController。如果它在它们之下,那么您应该对它们进行子类化以覆盖方向方法或创建类别以覆盖它们。那应该可以正常工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 2012-12-13
      • 1970-01-01
      相关资源
      最近更新 更多