iOS 6横向和纵向方向问题

4

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

在我的.xib中有两个视图,一个是竖屏模式,另一个是横屏模式。

在我的.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更新后一切都乱了。

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

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

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

我的其他使用storyboard的视图也是如此。它们是纵向的,总是这样显示,现在它们旋转,缩小一切,使我的应用程序变得糟糕。

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

谢谢。

3个回答

13

我认为我有一个解决方法。它很丑陋,但是有效...

随着iOS6的推出,苹果现在建议使用两个不同的XIB文件来在竖屏和横屏视图之间进行切换。

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

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两种解决方案,可以使用#ifdef宏来在控制器中包含两种代码。

祝好


1

无需发送和接收通知:

在您的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

0

虽然回答已经很晚了,但我认为我应该与您分享一下这个问题,

我也遇到了同样的问题。

shouldAutorotateToInterfaceOrientation 在 iOS 6 及以后版本中已被弃用。

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

而且非常重要的是,您需要确保在应用程序委托的 applicationDidFinishLaunching 方法中设置 根控制器,而不是仅仅将视图控制器的视图(或导航控制器或选项卡控制器,具体取决于您使用的内容)作为子视图添加到窗口中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接