LoginSignup
12
12

More than 5 years have passed since last update.

ios7対応のLandscape(横向き)のアプリを作るときの注意点

Last updated at Posted at 2015-02-20

image

Device OrientationをLandscapeのみに設定することで、Landscapeで起動するアプリを作成できます。弊社の絵本アプリ PIBO(ピーボ)でもそのような設定をしていますが、ios7以前に対応する際は画面サイズの取得に注意が必要です。

ios7での注意点

・self.view.frame.sizeは縦サイズを返す
・UIScreen.mainScreen().boundsも縦サイズを返す
・viewDidLoadと、viewWillAppearではself.view.bounds.sizeも縦サイズを返す
・PortraitからLandscapeに回転した場合は、self.view.bounds.sizeでは正しいサイズが取れます。

結論 ios7以前は下記のscreenSize()を使うのが良い

    func screenSize() -> CGSize {
        let screenSize = UIScreen.mainScreen().bounds.size
        if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
            return CGSizeMake(screenSize.height, screenSize.width)
        }
        return screenSize
    }

ios7とios8の各タイミングと画面サイズの違い

ios7で横向き起動時

*上記のscreenSize()がCorrect screenSize()です。

viewDidLoad
----------------
 frame                        w320.0 h:568.0
 bounds                       w320.0 h:568.0
 UIScreen.mainScreen().bounds w320.0 h:568.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewWillAppear
----------------
 frame                        w320.0 h:568.0
 bounds                       w320.0 h:568.0
 UIScreen.mainScreen().bounds w320.0 h:568.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewDidLayoutSubviews
----------------
 frame                        w320.0 h:568.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w320.0 h:568.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewDidAppear
----------------
 frame                        w320.0 h:568.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w320.0 h:568.0
 Correct screenSize()         w568.0 h:320.0
----------------

ios8で横向き起動時

viewDidLoad
----------------
 frame                        w568.0 h:320.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w568.0 h:320.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewWillAppear
----------------
 frame                        w568.0 h:320.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w568.0 h:320.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewDidLayoutSubviews
----------------
 frame                        w568.0 h:320.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w568.0 h:320.0
 Correct screenSize()         w568.0 h:320.0
----------------

viewDidAppear
----------------
 frame                        w568.0 h:320.0
 bounds                       w568.0 h:320.0
 UIScreen.mainScreen().bounds w568.0 h:320.0
 Correct screenSize()         w568.0 h:320.0
----------------

テストコード

下記コードで上記結果をお試し出来ます。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        printSizes("viewDidLoad")
    }

    override func viewWillAppear(animated: Bool) {
        printSizes("viewWillAppear")
    }

    override func viewDidLayoutSubviews() {
        printSizes("viewDidLayoutSubviews")
    }

    override func viewDidAppear(animated: Bool) {
        printSizes("viewDidAppear")
    }


    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
        if UIDevice.currentDevice().orientation.isLandscape {
            printSizes("isLandscape")
        }else if UIDevice.currentDevice().orientation.isPortrait {
            printSizes("isPortrait")
        }
    }

    func printSizes(s: String){
        println(s)
        println("----------------")
        println(" frame                        w\(self.view.frame.width) h:\(self.view.frame.height)")
        println(" bounds                       w\(self.view.bounds.width) h:\(self.view.bounds.height)")
        println(" UIScreen.mainScreen().bounds w\(UIScreen.mainScreen().bounds.width) h:\(UIScreen.mainScreen().bounds.height)")
        println(" Correct screenSize()         w\(self.screenSize().width) h:\(self.screenSize().height)")
        println("----------------")
        println()
    }

    func screenSize() -> CGSize {
        let screenSize = UIScreen.mainScreen().bounds.size
        if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
            return CGSizeMake(screenSize.height, screenSize.width)
        }
        return screenSize
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

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