【问题标题】:Change ViewControllers on Orientation change in runtime在运行时更改方向更改 ViewControllers
【发布时间】:2013-12-08 11:32:31
【问题描述】:

我想更改屏幕上的 ViewControllers 方向更改

我只有一个类 ViewController 和两个 StoryboardBased UIViewControllers。

  UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
    }

此代码可用于在方向更改时加载不同的 xib。但是如何在方向改变时在 Xib 文件中加载不同的控制器?

【问题讨论】:

    标签: ios objective-c ipad cocoa-touch


    【解决方案1】:

    你可以这样做

       - (void)viewDidLoad
         {
              [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];   
         }
    
         -(void)orientationChanged:(NSNotification *)notification
          {
               UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    
               if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
               {
                    [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];            
               }
               else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
               {
                    [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
               }
          }
    

    希望对你有所帮助......

    【讨论】:

      【解决方案2】:

      当您采用这种方法时,您不会在 xib 中加载不同的视图控制器。一个 xib 代表一个视图及其子视图。

      但是,我会采用另一种方法。我将创建一个视图 A(“主机”视图控制器的 self.view),它只有两个全屏容器视图,一个用于横向,一个用于纵向。 只需使两者都自动调整大小,以便它们始终占据全屏。

      在轮换事件时,我应该相应地隐藏和取消隐藏它们。

      但是为什么您认为每次旋转都需要单独的视图控制器?

      【讨论】:

      • 那动画呢,会不会很奇怪?
      • 您可以为隐藏和取消隐藏设置动画。你试过了吗?
      【解决方案3】:

      这里最好的方法可能是使用view controller containment api。你应该做的是有一个包装视图控制器,它嵌入来自 2 个视图控制器的两个视图(每个方向一个),并使用 iOS API 提供的方法 respond to orientation change 在视图之间切换

      【讨论】:

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