インターフェース全体を1つのストーリーボードにまとめました。ほとんどのViewControllerが縦向きのみをサポートし、カップルのみがすべての向きをサポートするようにするにはどうすればよいですか。りんごの新しい自動回転システムがわかりません。また、これをiOS 5と逆互換にするにはどうすればよいですか?
6 に答える
Navigation Controller サブクラスで、決定をスタック内の最上位のビュー コントローラーに転送します。
-(NSUInteger) supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
このための別のストーリーボード ファイルを作成することは、最悪の方法です。アプリの更新と同期させるためのメンテナンスの悪夢になります。
WWDC'2012 のビデオ、特に「要点」セクションでローテーションについて説明しているビデオをご覧ください。
「T Evolution of View Controllers in iOS」と呼ばれるものは、回転に関する主題 (およびさまざまな iOS バージョンと互換性を持たせる方法) を、説明とコード サンプルを使用して詳細に説明しており、Apple よりもうまく説明できませんでした。 )
さて、私はこれを回避する方法をちょっとハックしました。ここに私のサブクラス化された NavController.h があります
#import "RootVC.h"
@implementation RootVC
-(BOOL)shouldAutorotate {
def = [NSUserDefaults standardUserDefaults];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
if ([def integerForKey:@"Should Rotate"] == 1) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskPortrait;
}
@end
各ビューコントローラーのデフォルトで @"Should Rotate" に整数を設定するだけです
- (void)viewWillAppear:(BOOL)animated
魔法のように機能します!
これが私のサブクラス化された RootNavController.h です
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger) supportedInterfaceOrientations
{
if (self.topViewController.view.superview)
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
これは、iOS 5 デバイスと iOS 6 デバイス (および iPhone 5) の両方をサポートするために私が使用したハックです。プレフィックス ファイルに、次の #define を追加します。
#ifdef __IPHONE_6_0 // Only do the rotation fix if we are building with iOS 6 API
@protocol DeprecatedRotationSupported
@optional
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation;
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
@end
#define shouldAutorotateToInterface_fixed shouldAutorotate \
{ \
UIViewController <DeprecatedRotationSupported> *selfTyped = (UIViewController <DeprecatedRotationSupported> *) self; \
\
if(![self respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) \
return NO; \
int optionCount = 0; \
for(UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait; orientation <= UIDeviceOrientationLandscapeLeft; orientation++) \
{ \
if(![selfTyped shouldAutorotateToInterfaceOrientation:orientation]) continue; \
if(optionCount==1) return YES; \
optionCount++; \
} \
return NO; \
} \
\
- (NSUInteger)supportedInterfaceOrientations \
{ \
UIViewController <DeprecatedRotationSupported> *selfTyped = (UIViewController <DeprecatedRotationSupported> *) self; \
\
if(![self respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) return UIInterfaceOrientationMaskPortrait; \
\
NSUInteger supported = 0; \
\
if([selfTyped shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]) supported |= UIInterfaceOrientationMaskPortrait; \
if([selfTyped shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft]) supported |= UIInterfaceOrientationMaskLandscapeLeft; \
if([selfTyped shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight]) supported |= UIInterfaceOrientationMaskLandscapeRight; \
if([selfTyped shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortraitUpsideDown]) supported |= UIInterfaceOrientationMaskPortraitUpsideDown; \
return supported; \
} \
\
- (BOOL)shouldAutorotateToInterfaceOrientation
#else // We are building with the older API, leave shouldAutorotateToInterfaceOrientation alone.
#define shouldAutorotateToInterface_fixed shouldAutorotateToInterfaceOrientation
#endif // __IPHONE_6_0
次に、 shouldAutorotateToInterfaceOrientation: を使用する場所では、代わりに shouldAutorotateToInterface_fixed: を使用します。
ネイティブの rootViewController を使用している場合は、iOS5 を適切にサポートするように拡張する必要があります。これは、上記の WWDC ビデオで述べられています。
基本的に、UINavigationViewController を使用している場合は、それを拡張してその- (NSUInteger)supportedInterfaceOrientations
メソッドをオーバーライドします。そこで、あなたの子供たちを調べて (あなたが持っているインターフェイスの向きについて古いメソッドを照会することができます)、何をサポートすべきかを判断し、正しい値を返します。
下位互換性を保つためにこれを行う必要がある理由は 2 つあります。親は、その子がどの方向にある必要があるかを決定する必要があります。これらの変更はすべて、新しい AutoLayout のものと一致しています。
ナビゲーション コントローラーを拡張したら、ストーリーボードに移動します -> ルート コントローラー (この場合はナビゲーション コントローラー) を選択します -> クラスを新しく作成したクラスに設定します。
HTH