7

回避策を見つけたように見えるので、これは何よりも観察です...

以下のコードは、UINavigationController スタックにプッシュされたコントローラーにある場合、機能しません。この状況では、[UIDevice currentDevice].orientation一貫して が返されますUIDeviceOrientationUnknown

-(void)layoutAccordingToOrientation {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    NSLog(@"layoutAccordingToOrientation/orientation==%i", orientation);
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        :
        _detailToolbar.frame = CGRectMake(0, 660, 1024, 44);
    } else {
        :
        _detailToolbar.frame = CGRectMake(0, 916, 768, 44);
    }
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self layoutAccordingToOrientation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

次の呼び出し行われました。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

このUIDeviceOrientationUnknown問題を回避するために、代わりに以下を使用するようになりました。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
    etc.
} else {
    etc.
}

...毎回動作します。

それでも、プッシュされたView Controllerのコンテキストで最初のバリエーションが機能しない理由がわかりません。アイデアはありますか?単なるバグですか?

4

1 に答える 1

3

ここでいくつかのことを考えることができましたが、それらがあなたが探しているものかどうかはわかりません...

まず、アプリが起動されると、通常、UIDeviceOrientationUnknownパフォーマンス上の理由からデバイスの向きが返されます。試してみることができることの 1 つは、サブビューを .nib 内またはストーリーボードにレイアウトし、正しい自動サイズ変更オプションを使用して、iOS に任せることです。

第二に、あなたが私のような人なら、コンピューターのすぐ横にあるテーブルの上に置かれたデバイスでテストしているかもしれません。まあ、その場合、あなたは物を手に入れるつもりはありませんUIDeviceOrientationUnknown。実際にテストする必要がありますUIDeviceOrientationIsValidInterfaceOrientation([[UIDevice currentDevice] orientation])。たとえば、デバイスが仰向けになっている場合、そのステートメントに yes を返します。これは、 を使用するUIInterfaceOrientationIsLandscape(orientation)と、間違った理由で正しい結果が得られることも意味します。UIInterfaceOrientationIsLandscapeが false を返しているのは、デバイスの向きがではUIDeviceOrientationPortraitなく、無効だからです。

これが役に立ったかどうかはわかりませんが、それを理解するのにしばらく時間がかかりました。この情報を共有するのはいいことだと思いました! :)

于 2012-08-16T22:03:41.217 に答える