iOS 6横向和纵向

Pet*_*ter 4 device-orientation ios6 xcode4.5

我有一个表格,里面有一个来自plist文件的大量列表,点击每个文件会带你到一个新的视图,一个xib.

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

在我的文件中我有这个:

IBOutlet UIView *portraitView;  
IBOutlet UIView *landscapeView;

@property (nonatomic, retain) IBOutlet UIView *portraitView;  
@property (nonatomic, retain) IBOutlet UIView *landscapeView;
Run Code Online (Sandbox Code Playgroud)

在我的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
Run Code Online (Sandbox Code Playgroud)

一切都在iOS 5中完美运行,在需要时显示风景或肖像.

现在有了iOS 6更新,一切都很乱.

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

如果我做同样但开始景观,它显示肖像.

所以,现在的方向不适用于任何事情.

使用storyboard的其他视图也是如此.它们是肖像并且总是这样显示,现在它们旋转,收缩所有内容并将我的应用程序留作垃圾.

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

谢谢.

Xav*_*lla 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];
 }
Run Code Online (Sandbox Code Playgroud)

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];
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我建议你创建一个自定义的UIViewController类并从这个类继承 - 以保存冗余代码.如果要同时支持ios5和ios6的解决方案,可以使用#endif宏在控制器中包含这两个代码.

干杯