2

ナビゲーションコントローラーアプリがあります。最初に FirstViewController (縦方向をサポート) をプッシュし、次に SecondViewController (すべての方向をサポート) をプッシュします。SecondViewController のランドスケープ モードで [戻る] ボタンを押すと、FirstViewController がランドスケープ モードで表示されます。そのため、ナビゲーション ビューを手動で回転させますが、setStatusBarOrientation を Portrait に設定したい場合 (最初のビュー コントローラーは縦モードでのみ表示されます)、View Controller の向きは横のままで、デバイスを縦モードに回転させても、向きは横向きのままです。FirstViewControllerの私のコードは次のとおりです。

- (void)viewWillAppear:(BOOL)animated
{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            prevInterfaceOrientation = UIInterfaceOrientationLandscapeLeft;
            self.navigationController.view.transform = CGAffineTransformIdentity;
            self.navigationController.view.transform =
            CGAffineTransformMakeRotation(degreesToRadians(90));
        }
        else if (self.interfaceOrientation ==
                 UIInterfaceOrientationLandscapeRight) {
            prevInterfaceOrientation = UIInterfaceOrientationLandscapeRight;
            self.navigationController.view.transform = CGAffineTransformIdentity;
            self.navigationController.view.transform =
            CGAffineTransformMakeRotation(degreesToRadians(-90));
        }
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
        [self.tableViewDetail reloadData];
    }


}

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

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{ 
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    {
        if (prevInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            self.navigationController.view.transform = CGAffineTransformIdentity;
        }
        else if (prevInterfaceOrientation ==
                 UIInterfaceOrientationLandscapeRight) {
            self.navigationController.view.transform = CGAffineTransformIdentity;
        }
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        [self.tableViewDetail reloadData];
    }
}

私も使用しようとしました:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(prevInterfaceOrientation))
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    }
}

しかし、self.interfaceOrientation は、縦向きに回転しても横向きのままです。しかし、FirstViewController が縦向きのみをサポートしていることをユーザーが確認できるようにするには、手動でビューを縦向きモードに回転させる必要があります。SecondViewController のビューを (モーダル ウィンドウのように) MainWindow に配置するオプションがありますが、Apple に setStatusBarOrientation メソッドがある場合、この問題を正しく解決する必要があるように思われるため、この考えは好きではありません。

4

3 に答える 3

4

私は変換を取り除き、使用します

      [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];

これは、ビュー スタックの強制的な再描画と組み合わせて実行されます。これは、最初のコントローラーの viewDidAppear に以下を追加することで実行できます (viewWillApear では機能しません)。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

       UIWindow *window = [[UIApplication sharedApplication] keyWindow];
       if ([window.subviews count] > 0) {
           UIView *view = [window.subviews objectAtIndex:0];
           [view removeFromSuperview];
           [window insertSubview:view atIndex:0];
       }
       else {
           DLog(@"NO view to force rotate?");
       }

残念ながら、これを行うとトランジション アニメーションがきれいに表示されないため、ポートレート画面のスナップショットを撮り、これをビューに重ねてから、別のアニメーションでフェードアウトすることをお勧めします。

于 2012-06-15T07:07:17.580 に答える
0

Steven Veltema の答えはうまくいきませんでした。

すべての向きが許可されているビューコントローラーが1つあり、残りは縦向きのみをサポートしていました。最初のView Controllerを横向きにして別のView Controllerに移動したとき、あなたが経験しているように向きが更新されませんでした。

ビューをリロードして向きを修正する別のトリックを見つけました。表示されていないモーダル ビュー コントローラーを追加するだけです。これを他のすべてのビューに追加します。

- (void)viewDidAppear:(BOOL)animated
{
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        UIViewController * viewController = [[UIViewController alloc] init];
        [self presentModalViewController:viewController animated:NO];
        [viewController dismissModalViewControllerAnimated:NO];
    }

    [super viewDidAppear:animated];
    ....
}
于 2013-03-28T12:27:16.193 に答える