【问题标题】:iOS 6 landscape and portrait orientationiOS 6 横向和纵​​向
【发布时间】:2012-09-24 12:37:52
【问题描述】:

我有一个表格,其中包含来自 plist 文件的大量内容列表,单击其中的每一个都会将您带到一个新视图,即 xib。

我在 .xib 中有 2 个视图,一个用于纵向,一个用于横向

在我的 h 文件中,我有这个:

IBOutlet UIView *portraitView;  
IBOutlet UIView *landscapeView;

@property (nonatomic, retain) IBOutlet UIView *portraitView;  
@property (nonatomic, retain) IBOutlet UIView *landscapeView;

在我的 m 文件中:

[super viewDidLoad];  
    // Do any additional setup after loading the view from its nib.

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}

- (void) orientationChanged:(id)object
{
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation];

if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
    {  
        self.view = self.portraitView;  
    }  
    else
    {  
        self.view = self.landscapeView;  
    }  
}

- (void)viewDidUnload
{

    [super viewDidUnload];  
    // Release any retained subviews of the main view.  
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation   
{  
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {  

       self.view = portraitView;
    } else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

       self.view = landscapeView;  
    }  
    return YES;

}

@end

在 iOS 5 中一切正常,在需要时显示横向或纵向。

现在随着 iOS 6 的更新,一切都变得一团糟。

如果我在表格(纵向)视图中并单击一个项目,它会以纵向显示正确,如果我旋转到横向,则视图也会显示正确的视图,但是如果我回到表并选择另一个项目,它会显示纵向视图而不是横向视图。

如果我做同样的事情但从横向开始,它会显示纵向。

所以,现在方向对任何事情都不起作用。

我使用故事板的其他视图也是如此。它们是纵向的并且总是这样显示,现在它们会旋转、缩小所有内容并将我的应用程序作为垃圾。

1- 如何修复 .xib 方向的问题?
2-如何修复故事板方向? (它们是静态的,现在一切都在旋转(根本没有代码))

谢谢。

【问题讨论】:

    标签: ios6 device-orientation xcode4.5


    【解决方案1】:

    我认为我有一个解决办法。这很丑陋,但它正在工作...... 对于 iOS6,Apple 现在建议使用 2 个不同的 XIB 文件在纵向和横向视图之间切换。

    但是,如果您想通过在 2 个 UIView IBOutlet 之间“切换”来使用 iOS 5.0 中允许的先前方法,您可以尝试我的丑陋工作解决方案。这个想法是根据方向旋转视图。

    1) 在你的 viewDidLoad 中,订阅方向通知:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
     }
    

    2) 添加通知调用的方法:

    -(void)orientationChanged:(NSNotification *)object{
        NSLog(@"orientation change");
        UIDeviceOrientation deviceOrientation = [[object object] orientation];
        if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            self.view = self.potraitView;
            if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){
                NSLog(@"Changed Orientation To PortraitUpsideDown");
                [self portraitUpsideDownOrientation];
            }else{
                NSLog(@"Changed Orientation To Portrait");
                [self portraitOrientation];
            }
        }else{
            self.view = self.landscapeView;
            if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
                NSLog(@"Changed Orientation To Landscape left");
                [self landscapeLeftOrientation];
            }else{
                NSLog(@"Changed Orientation To Landscape right");
                [self landscapeRightOrientation];
            }
    
        }
    }
    

    3) 最后,为每个方向添加旋转方法:

    -(void)landscapeLeftOrientation{
    
        // Rotates the view.
        CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2));
        self.view.transform = transform;
        // Repositions and resizes the view.
        CGRect contentRect = CGRectMake(0, 0, 480, 320);
        self.view.bounds = contentRect;
    }
    -(void)landscapeRightOrientation{
    
        // Rotates the view.
        CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
        self.view.transform = transform;
        // Repositions and resizes the view.
        CGRect contentRect = CGRectMake(0, 0, 480, 320);
        self.view.bounds = contentRect;
    }
    -(void)portraitOrientation{
    
        // Rotates the view.
        CGAffineTransform transform = CGAffineTransformMakeRotation(0);
        self.view.transform = transform;
        // Repositions and resizes the view.
        CGRect contentRect = CGRectMake(0, 0, 320, 480);
        self.view.bounds = contentRect;
    }
    -(void)portraitUpsideDownOrientation{
    
        // Rotates the view.
        CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159);
        self.view.transform = transform;
        // Repositions and resizes the view.
        CGRect contentRect = CGRectMake(0, 0, 320, 480);
        self.view.bounds = contentRect;
    }
    

    我建议您创建一个自定义 UIViewController 类并从该类继承以节省冗余代码。 如果您想同时支持 ios5 和 ios6 的解决方案,您可以使用 #endif 宏将这两个代码都包含在您的控制器中。

    干杯

    【讨论】:

      【解决方案2】:

      无需发送和接收通知:

      在您的 appdelegate.m 中使用以下方法

      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
      

      总是被调用来检查窗口的方向, 所以一个简单的方法是在你的 appdelegate.m 中包含下面描述的代码

      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
      
          NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
      
          if(self.window.rootViewController){
              UIViewController *presentedViewController ;
              if ([self.window.rootViewController isKindOfClass:([UINavigationController class])])
              {
                  presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
              }
              else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
                  UITabBarController *controller = (UITabBarController*)self.window.rootViewController;
      
                  id selectedController =  [controller presentedViewController];
      
                  if (!selectedController) {
                      selectedController = [controller selectedViewController];
                  }
      
                      if ([selectedController isKindOfClass:([UINavigationController class])])
                      {
                          presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject];
                      }
                      else{
                          presentedViewController = selectedController;
                      }
              }
              else
              {
                  presentedViewController = self.window.rootViewController;
              }
      
              if ([presentedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
                  orientations = [presentedViewController supportedInterfaceOrientations];
              }
          }
      
          return orientations;
      }
      

      并实施

      - (NSUInteger)supportedInterfaceOrientations
      

      在各自的视图控制器中

      - (NSUInteger)supportedInterfaceOrientations{
          return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
      }
      

      要针对方向变化执行突然动作,请执行以下方法

      - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      

      【讨论】:

        【解决方案3】:

        这个答案已经很晚了,我还是想我应该和你分享一下,以防万一,

        我也遇到了同样的问题。

        shouldAutorotateToInterfaceOrientation 从 iOS 6 开始已弃用。

        您需要将此方法与新的 supportedInterfaceOrientationsshouldAutorotate 方法并行。

        而且非常非常重要,您需要确保在您的应用代理的 applicationDidFinishLaunching 方法中设置 根控制器,而不是简单地添加视图控制器的视图(或导航控制器或 tabBar 控制器,具体取决于您使用的内容)作为窗口的子视图。

        【讨论】:

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