LoginSignup
12
10

More than 5 years have passed since last update.

PageMenuで1つ目のUIViewControllerのレイアウトが崩れる

Last updated at Posted at 2015-05-23

SwiftのライブラリであるPageMenuを使って横スクロールでUIViewControllerを複数閲覧出来るアプリを作っていたのですが、どうも1つ目のVCだけAutoLayoutが思うように動いてくれませんでした。

調べたところ、PageMenu本家のDemoでもAutoLayoutを使用したUIViewControllerを1つ目に設定するとレイアウト崩れするようです。

before.png

"UIViewController"の文字がAutoLayoutでHorizontal Center in Container指定されているはずが中央からズレています。
ちなみに、別のVCへ移動して戻ってくると

after.png

今度はぴったり中央に合っています。

どうも1つ目のVCを初めて表示したときのサイズが上のメニューバー分大きくなっているようです。
根本的解決では無いような気がしますが仕方なく以下のコードをVCに追加して手動でリサイズしました。

TestViewController.swift
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let superSize   = self.view.superview!.frame.size
    let rect        = self.view.frame
    let size        = self.view.frame.size

    self.view.frame.size = CGSizeMake(size.width, superSize.height - rect.origin.y)
}

これで無事正しいサイズで最初から表示され、レイアウト崩れは起こらなくなりました。

12
10
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
12
10