2017-01-26 2 views
1

특정 ViewController에서 방향 변경을 감지하려고합니다. 알림 및 사용자 지정 함수를 사용합니다. 알림이 변경 될 때마다 iPhone 7 (iOS 10.2 실행)을 사용하면 회전 된 기능이 호출됩니다. 그러나 iPhone 6 (실행 9.3) 또는 iPhone 6 (10.0 실행 중)을 사용하는 동안 화면이로드 될 때 기능은 , 전화 번호는으로 만 호출되지만 전화를 돌릴 때는 그렇지 않습니다.UIDeviceOrientationDidChange는 iPhone 7에서만 작동합니다.

코드 :

func rotated() { 
    if UIDevice.current.orientation.isLandscape { 
     print("Landscape") 
    } else { 
     print("Portrait") 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad()   
    UIDevice.current.beginGeneratingDeviceOrientationNotifications() 
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
} 

답변

0

viewWillAppear 함수에서 알림을 설정하고 그것을 잘 작동합니다. 시뮬레이터에서 사용할 수있는 장치에서 잘 작동합니다. 사실 View DidLoad 함수는 뷰 컨트롤러가 처음 나타날 때 한 번만 호출됩니다. 뷰 컨트롤러가로드 될 때마다 뷰가 표시되는 반면에 호출됩니다.

override func viewWillAppear(_ animated: Bool) { 
super.viewWillAppear(animated) 
UIDevice.current.beginGeneratingDeviceOrientationNotifications() 
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
} 
관련 문제