2016-06-03 1 views
3

내 앱이 iPad의 모든 방향에서 작동하고, iPhone 6 Plus에서 가로 및 세로를 지원하고, 다른 장치에서만 세로 방향을 지원하도록하고 싶습니다.iPhone 6 Plus에서 방향이 잘못 되었습니까?

그러나 iPhone 6/6s Plus에서는 올바르게 작동하지 않습니다. 회전이 이상하고 뷰 컨트롤러가 종종 잘못된 방향으로 나타납니다.

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 

    let height = window?.bounds.height 

    if height > 736.0 { 
     // iPad 
     return .All 
    } else if height == 736.0 { 
     // 5.5" iPhones 
     return .AllButUpsideDown 
    } else { 
     // 4.7", 4", 3.5" iPhones 
     return .Portrait 
    } 

} 

이 작업을 수행하는 올바른 방법은 무엇입니까 :

이 내가 현재 내 AppDelegate.swift에있는 무엇인가?

답변

2

적절한 인터페이스 방향을 설정하는 데 사용할 수있는 여러 가지 방법이 있습니다. 우선, 하드 코딩 된 높이를 사용하면 버그가 발생하기 쉽고 Apple에서는 이러한 유형의 장치 검사를 강력히 권장하지 않습니다. 대신 특성 수집을 사용합니다. UITraitCollection은 iOS 8에 도입 된 API이며 장치 관용구, 표시 크기 및 크기 클래스에 대한 정보를 포함합니다. 특성 컬렉션은 UIWindowUIViewController 개체에 액세스 할 수 있습니다.

예에서 기기가 userInterfaceIdiom 속성을 사용하여 기기인지 먼저 확인한 다음 displayScale (6.0)을 확인합니다 (3.0).

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 

     if window?.traitCollection.userInterfaceIdiom == .Pad { 
      // Check for iPad 
      return .All 
     } else if window?.traitCollection.displayScale == 3.0 { 
      // iPhone 6/6s Plus is currently only iPhone with display scale of 3.0 
      return [.Portrait, .Landscape] 
     } else { 
      // Return Portrait for all other devices 
      return .Portrait 
     } 
    } 

당신은 내가 읽어 보시기 바랍니다 특성 컬렉션과 크기 클래스에 대한 자세한 내용을 알고 싶다면 공식 Apple documentation

관련 문제