【问题标题】:How to rotate view from portrait to landscape mode如何将视图从纵向模式旋转到横向模式
【发布时间】:2013-12-16 08:08:08
【问题描述】:

我的应用程序中的方向有问题。假设我有两个视图(带有专用视图控制器):

  • 首先应该纵向显示(显示正确)
  • 应该横向显示(显示不正确)

它被缩小并以纵向显示(如下面的第二张图片所示)。 当我将设备水平旋转并返回纵向时,一切正常。但是在推送视图后它显示不正确(下图)。我该如何解决这个问题?

我使用继承自 UINavigatorControler 并实现三个方法的 CustomNavigationController:

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}

在应用程序委托中,我以这种方式初始化控制器:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];

第一个视图控制器以这种方式实现方向功能:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait)
        return YES;

    return NO;
}

第二个视图控制器以这种方式实现方向功能:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;
}

【问题讨论】:

    标签: ios iphone objective-c view orientation


    【解决方案1】:

    hi 声明一个全局变量 BOOL isLandScape ;

    initialize it as isLandScape=NO;
    
    
       - (NSUInteger)supportedInterfaceOrientations
        {
        return UIInterfaceOrientationMaskLandscapeRight;
      }
    
    
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
    
      if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
    {
    
          isLandScape=YES;
    
            return YES;
    
    }
    
    else
    
    {
    
    isLandScape=NO;
    
     return NO;
    
    }
    
    yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);
    
    
    }
    

    【讨论】:

      【解决方案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:
      

      【讨论】:

        【解决方案3】:

        为此,您可以使用此功能:

        [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
        

        您可以在任何地方使用它,但在应用程序委托中(在 didFinishLaunchingWithOptions 中)我必须输入以下代码:

        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        

        效果很好!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-11
          相关资源
          最近更新 更多