1

I know that this question was asked before and tried to implement the suggested solution, but it doesn't work for me. Perhaps I am doing smth wrong. Do you have any idea?

Here is my code:

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
        }
      }
    }
 }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Roman
  • 759
  • 2
  • 9
  • 23
  • Possible duplicate of [How to get current orientation of device programatically in iOS 6?](http://stackoverflow.com/questions/13836578/how-to-get-current-orientation-of-device-programatically-in-ios-6) – TajyMany Jan 15 '17 at 09:37
  • I tried to follow these suggestions but could not implement them in my case. – Roman Jan 15 '17 at 10:13

2 Answers2

3

You have nested functions - that won't work. Change it to this:

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

In fact, you can get rid of the override for viewWillLayoutSubviews with the code you've shown.

1

** to those that need additional help see recommendations below ** this function will be called before the change in size and but you will know it from the arguments

Swift 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)")

    }

}
Hemang
  • 26,840
  • 19
  • 119
  • 186