4

サイドバーを含み、コンテンツ領域のビューコントローラーを交換する非常に基本的なコンテナービューがあります(UISplitViewを考えてみてください。ただし、小さなアイコンのサイドバー/垂直UITabBarがあります)。

コンテナビューコントローラはautoLayoutを使用し、回転すると正しくサイズ変更されます。コンテンツviewController1は自動レイアウトを使用し、IBで作成されているため、xibファイルがあります。コンテンツviewController2はUITableViewControllerを継承し、xibを使用しません。

ビューコントローラー1をルートビューコントローラーとして割り当てて回転すると、サイズ変更が機能し、viewController1で取得するコールバックは次のようになります。

  • willRotateToInterfaceOrientation
  • updateViewConstraints
  • viewWillLayoutSubviews
  • didRotateFromInterfaceOrientation

ただし、コンテナビューコントローラをルートビューコントローラとして割り当て、viewController 1をロードして回転すると、サイズ変更が機能しません。そして、viewController1内で次のコールバックのみを取得します。

  • willRotateToInterfaceOrientation
  • didRotateFromInterfaceOrientation

ビューコントローラーコンテナ内で、ビューコントローラーを交換する方法は次のとおりです。

[self addChildViewController:toViewController];
[toViewController didMoveToParentViewController:self];

// Remove the old view controller
[fromViewController willMoveToParentViewController:nil];
[fromViewController.view removeFromSuperview];
[fromViewController removeFromParentViewController];

// Add the new view
[self.contentContainerView addSubview:toViewController.view];

これで、ローテーションが発生しようとしているというコールバックを取得しましたが、updateViewConstraintsもviewWillLayoutSubviewsも呼び出されていないようです。これは、サイズ変更が行われない理由を説明していますが、View Controllerをコンテナービューに配置すると、これらのメソッドが呼び出されないのはなぜですか?

また、両方のコンテナで明示的にYESを返そうとしました

shouldAutomaticallyForwardAppearanceMethods

shouldAutomaticallyForwardAppearanceMethods

これはすでにデフォルトになっているはずですが。

また、IBで作成されていないView Controller(View Controller 2)は、コンテナ内で回転すると正しくサイズ変更されます。ただし、これには明示的にNSLayoutConstraintsを使用していないため、回転時のサイズ変更にはデフォルトでSpringsとStrutsが使用されていると思われます。

回転時に自動レイアウトビューコントローラーのサイズを正しく変更するには、ビューコントローラーコンテナーで他のイベントを転送する必要がありますか?

4

2 に答える 2

3

OK、ViewControllerコンテナにこのメソッドがなかったと思います。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.contentViewController.view.frame = self.contentContainerView.bounds;
}

これは回転時に正しくサイズ変更されるようになりましたが、それでもトリガーされません

updateViewConstraints

私の子ビューコントローラで。面白い

于 2012-11-03T08:24:57.493 に答える
0

iOS8がupdateViewConstraintsを呼び出しているようです。しかし、iOS7はそうではありませんでした。これをiOS7で呼び出すには、次のようにsetNeedsUpdateConstraintsを呼び出します。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];

    BOOL isiOS7 = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1;
    if (isiOS7) {
        // Trigger a call to updateViewConstraints
        [self.view setNeedsUpdateConstraints];
    }
}

updateLayoutConstraintsで、レイアウトする方向を確認する良い方法は、ステータスバーの方向を確認することです。これは7と8で機能します。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
BOOL layoutAsLandscape = UIInterfaceOrientationIsLandscape(orientation);
于 2014-12-01T20:58:46.490 に答える