iOS 6横向和纵向

时间:2012-09-24 12:37:52

标签: ios6 device-orientation xcode4.5

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

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

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

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-如何修复故事板方向? (它们是静态的,现在一切都旋转了(根本没有代码))

感谢。

3 个答案:

答案 0 :(得分:13)

我认为我有一个解决方法。这很难看,但它正在...... 使用iOS6 Apple建议现在使用2个差异XIB文件在纵向和纵向之间切换景观观。

但是如果你想通过在两个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宏在控制器中包含这两个代码。

干杯

答案 1 :(得分: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

答案 2 :(得分:0)

这个答案已经很晚了,但我认为我应该和你分享这个,以防万一,

我遇到了同样的问题。

从iOS 6开始,不推荐使用

shouldAutorotateToInterfaceOrientation

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

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