2015-01-15 52 views

回答

0

爲iPad

[[UIDevice currentDevice] orientation]; 
+0

得到它,但我很好奇,如果這仍然一樣'UIInterfaceOrientation' – cocoapriest 2015-01-15 15:47:26

+0

不,它不是。有一種醜陋的方式可能會導致一個單獨的答案,因爲它太大而不能發表評論。 – 2016-12-30 13:09:37

9

您可以使用[UIApplication sharedApplication].statusBarOrientation

注重的是[[UIDevice currentDevice] orientation]是不完全一樣您的應用程序旋轉。文檔:

討論: 的屬性的值是表示所述設備的當前取向恆定。該值表示設備的物理方向,可能與應用程序用戶界面的當前方向不同。有關可能值的說明,請參閱UIDeviceOrientation。

我會檢查statusBarOrientation是否工作。

1

UIApplication.sharedApplication()。statusBarOrientation工作,除非你是從

func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 

其中早,如果你正在使用viewWillTransitionToSize更新 設備換到新statusBarOrientation

因此基於取向調用它(而不是尺寸級別),你擰 ,並必須使用這樣的事情

extension UIViewController { 
    public var orientation : UIInterfaceOrientation? { 
     let orientation = UIDevice.currentDevice().orientation 
     var converted : UIInterfaceOrientation! 
     // UIApplication.sharedApplication().statusBarOrientation is trash cause it lags (being set to the value predating the transition reported by viewWillTransitionToSize) 
     switch orientation { 
     case .Portrait: // Device oriented vertically, home button on the bottom 
      converted = .Portrait 
     case .PortraitUpsideDown: // Device oriented vertically, home button on the top 
      converted = .PortraitUpsideDown 
     case .LandscapeLeft: // Device oriented horizontally, home button on the right 
      converted = .LandscapeLeft 
     case .LandscapeRight: // Device oriented horizontally, home button on the left 
      converted = .LandscapeRight 
     default: 
      // let the caller do the dirty dancing with the status bar orientation or whatever 
      return nil 
     } 
     return converted 
    } 
} 

,你會使用像這樣:

// willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) is deprecated. 
    // thusly I have to use the size classes method and compute interface orientation from the device orientation 
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
     super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) 
     guard let converted = orientation else { 
      //   alert("something else that I do not [need to] handle") 
      return 
     } 
     if converted.isLandscape { 
      adjustViewsForOrientation(converted, withTransitionCoordinator: coordinator) 
     } else if converted.isPortrait { 
      adjustViewsForOrientation(converted, withTransitionCoordinator: coordinator) 
     } 
    } 

我不知道如何擺脫這個勇敢的新世界的(吸住)undeprecating willRotateToInterfaceOrientation 他們與autoresizingMask一樣的工作方式有說服力的appple的短 (除了他們有意識,不貶低autoresizingMask ;-)

即或注入interfaceorientation作爲第三參數到 viewWillTransitionToSize(尺寸:CGSize,withTransitionCoordinator協調器:UIViewControllerTransitionCoordinator)

2

在斯威夫特你可以用

let orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation

相關問題