13

child を持つView ControllerをプログラムでセットアップしましたUIPageViewController。これにより、プログラムでセットアップされた他のさまざまなView Controllerが表示されます。これらのビュー コントローラーのビューは にtranslatesAutoresizingMaskIntoConstraints設定されてYESいますが、ページ ビュー コントローラーのビューは制約を使用して、自分自身を最上位のビュー コントローラーに配置します。

問題は、ユーザーがデバイスを回転させると、ページ ビュー コントローラーはフレームのサイズを変更しますが、子ビュー コントローラーは変更しないことです。によってdidRotateFromInterfaceOrientation、そのフレームはまだ古い方向からです。回転メソッドが子ビュー コントローラーで呼び出されることを確認しましたが、そのフレームは変更されません。

次のようにページ ビュー コントローラーを設定します。

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.view.backgroundColor = [UIColor clearColor];
self.pageViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
[self.pageViewController willMoveToParentViewController:self];
[self.view addSubview:self.pageViewController.view];
[self addChildViewController:self.pageViewController];
[self.pageViewController didMoveToParentViewController:self];

self.currentPageIndex = 0;

self.currentPageController = [self pageForIndex:self.currentPageIndex];
self.currentPageController.delegate = self;
[self.pageViewController setViewControllers:@[self.currentPageController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];

呼び出してみsetNeedsLayoutたのですが、回転アニメーションが不器用で、次にスワイプしたページのサイズも適切にリサイズされていません。

ページ ビュー コントローラーが子ビュー コントローラーのサイズを変更しないのはなぜですか? また、これを行うにはどうすればよいですか?

ありがとう!

4

2 に答える 2

6

まず、自動レイアウト、プログラムまたはストーリーボードですべてを設定します。次に、親ビュー コントローラーで向きの変更をキャッチし、UIPageViewController のビューを強制的に再度レイアウトします。

これは私の作業コードです (iOS 8.0 以降)。childController1制約を使用してすべてのレイアウトでストーリーボードに設定され、 でインスタンス化されまし[self.storyboard instantiateViewControllerWithIdentifier:@"ChildControllerId"]た。

- (void)createPageController
{
...
    pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    pageController.dataSource = self;
    [pageController setViewControllers:@[childController1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:pageController];
    [self.view addSubview:pageController.view];
    UIView *pageView = pageController.view;
    pageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
    [pageController didMoveToParentViewController:self];
...
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [pageController.view setNeedsLayout];
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
}

それが役に立てば幸い。

于 2015-08-11T17:52:42.683 に答える
-2

コントローラーのビューを別のコントローラーのビューに直接追加し、ローテーション メソッドとライフサイクル メソッドが呼び出されることを期待することは、最適なポリシーではありません。コントローラー階層に追加するとうまくいくと思いますが、それについてはあまりコメントできません。

タブバーナビゲーターでテストしたところ、UIPageViewController は、UIPageViewController に表示されていたコントローラーと共にイベントを取得しました。コントローラーを UINavigationController にプッシュすると、同じ結果が得られると思います。

ビューを別のコントローラーのビューに追加するよりも、より標準的な方法で UIPageVIewController を表示することをお勧めします。

于 2014-02-02T23:59:05.297 に答える