2017-01-15 1 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

가능한 복제 [프로그래밍 방식으로 아이폰 OS 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)") 

    } 

}