我创建了两个相同的故事板,除了一个包含纵向视图和一个包含横向视图。
我不想使用自动调整大小的蒙版,因为某些视图的布局在纵向和横向之间完全变化。过去我在代码中手动移动了视图上的控件,但这次我采用了一种更简单的方法:)
我找到的最接近的解决方案来自 'benyboariu' - Storyboardsorientation support for xCode 4.2?
这是我正在使用的代码,它在 iOS 5.x 上运行良好,但在 iOS 6.x 上运行良好。
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIDeviceOrientationUnknown)
{
if ([[UIScreen mainScreen] bounds].size.height > [[UIScreen mainScreen] bounds].size.width)
{
deviceOrientation = UIDeviceOrientationPortrait;
self.appDelegate.isShowingLandscapeView = NO;
}
else
{
deviceOrientation = UIDeviceOrientationLandscapeLeft;
self.appDelegate.isShowingLandscapeView = YES;
}
}
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !self.appDelegate.isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Landscape" bundle:[NSBundle mainBundle]];
UIViewController * landscape = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController-Landscape"];
self.appDelegate.isShowingLandscapeView = YES;
[UIView transitionWithView:landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
self.view = landscape.view;
} completion:NULL];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && self.appDelegate.isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Portrait" bundle:[NSBundle mainBundle]];
UIViewController * portrait = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
self.appDelegate.isShowingLandscapeView = NO;
[UIView transitionWithView:portrait.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
self.view = portrait.view;
} completion:NULL];
}
}
我在 iOS 6.x 上调试时遇到的错误是:
由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“一个视图一次最多只能与一个视图控制器相关联!查看 UIView: 0x108276b0; 帧 = (0 0; 568 268); 自动调整大小 = RM+BM;动画={位置=CABasicAnimation:0x10815c60;bounds=CABasicAnimation: 0x1082a5e0; }; layer = CALayer: 0x10827710 与 RootViewController: 0x10821f10 相关联。在将此视图与 RootViewController 关联之前清除此关联:0x961a150。
我通常将视图控制器与 NIB 断开链接以修复此类错误,但无法通过情节提要执行此操作。
有人有什么想法吗?