LoginSignup
12
14

More than 5 years have passed since last update.

[Swift 3.0] ViewController単位で画面の向きを制御する

Last updated at Posted at 2017-02-02

(おそらくSwift3.0から)デバイスの回転による画面の向きの制御を行うshouldAutorotatesupportedInterfaceOrientationsが、メソッドからreadonlyのプロパティに変わりました。

ソースはAPI Referenceの "Configuring the View Rotation Settings" のセクションをご参照ください。

したがって、ViewController単位でデバイスの回転による画面の向きを制御するには、これらのプロパティをオーバーライドしてgetで返したい値を設定することになります。
以下に、ViewControllerをPortrait固定にする例を示します。

FooViewController.swift
class HogeViewController: UIViewController {
    // 省略

    // 画面の自動回転をさせない
    override var shouldAutorotate: Bool {
        get {
            return false
        }
    }

    // 画面をPortraitに指定する
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            return .portrait
        }
    }

    // 省略
}
12
14
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
14