27
27

More than 5 years have passed since last update.

iOS8にて画面の向き変更時にレイアウト変更を行う場合

Last updated at Posted at 2014-11-11

iOS8にて画面の向き変更時にコードにてレイアウト変更を行う場合、該当のViewControllerのviewWillTransitionToSizeに下記の通り実装する。

※ポイント
viewWillTransitionToSizeにそのまま実装しても、サイズが不確定の状態の為、位置がおかしくなる。
そのためcoodinatorのanimatealongsideTransition内部でサイズ変更処理を行う必要がある。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if (size.width <= size.height) {
            // ここで縦レイアウトの位置修正を行う。
        }
        else {
            // ここで横レイアウトの位置修正を行う。
        }

    } completion:nil];
}

27
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
27