縦向きのビューと横向きのビューが含まれていることを除いて、両方とも同じ 2 つのストーリーボードを作成しました。
一部のビューのレイアウトは縦向きと横向きで完全に変わるため、自動サイズ変更マスクを使用したくありません。過去にコードでビューのコントロールを手動で移動しましたが、今回はより簡単な方法を求めていました:)
私が見つけた最も近い解決策は、「benyboariu」からのものでした - Storyboards orientation 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; アニメーション = { position=CABasicAnimation: 0x10815c60; 境界 = CABasicAnimation: 0x1082a5e0; }; layer = CALayer: 0x10827710 は RootViewController: 0x10821f10 に関連付けられています。このビューを RootViewController: 0x961a150 に関連付ける前に、この関連付けをクリアしてください。
通常、NIB からビュー コントローラーのリンクを解除してこの種のエラーを修正しますが、ストーリーボードでそれを行うことはできません。
誰でもアイデアはありますか?