2017-01-15 9 views
0

私はこの質問が以前に尋ねられ、提案されたソリューションを実装しようとしたことを知っていますが、それは私のためには機能しません。おそらく私は間違ったことをしているのかもしれない。あなたは何か考えていますか? - 動作しませんあなたは関数を入れ子にしている現在のデバイスの向きを取得

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var test: UILabel! 

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

    test.backgroundColor = UIColor.darkGray 
} 

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

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) // No need for semicolon 

    func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
     if UIDevice.current.orientation.isLandscape { 
      test.backgroundColor = UIColor.purple 
     } else { 
      test.backgroundColor = UIColor.blue 
     } 
     } 
    } 
} 
+0

[iOS 6でプログラムの現在の向きを取得するにはどうすればいいですか?](http://stackoverflow.com/questions/13836578/how-to-get-current-orientation-of-device-programatically- in-ios-6) – TajyMany

+0

私はこれらの提案に従おうとしましたが、私の場合は実装できませんでした。 – Roman

答えて

2

は、ここに私のコードです。このように変更します。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) // No need for semicolon 
} 
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
    super.viewWillTransition(to: size, with: coordinator) 
    if UIDevice.current.orientation.isLandscape { 
     test.backgroundColor = UIColor.purple 
    } else { 
     test.backgroundColor = UIColor.blue 
    } 
} 

は、実際には、あなたが示したコードとviewWillLayoutSubviewsのオーバーライドを取り除くことができます。この機能は、サイズの変更の前に呼び出されます** 次の推奨事項を参照してください追加の助けを必要とするものに

1

**とができますが、引数から

スウィフト3.xの+

それを知っているだろう
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    super.viewWillTransition(to: size, with: coordinator) 
    if UIDevice.current.orientation.isLandscape { 
     //Landscape 

      print("before transition") 
      print("w "+"\(width)") 
      print("h "+"\(height)") 
      print("to size") 
      print("w "+"\(size.width)") 
      print("h "+"\(size.height)") 

    } else { 
     //Portrait  

      print("before transition") 
      print("w "+"\(width)") 
      print("h "+"\(height)") 
      print("to size") 
      print("w "+"\(size.width)") 
      print("h "+"\(size.height)") 

    } 

} 
関連する問題