Skip to content

Instantly share code, notes, and snippets.

@eleev
Last active March 24, 2017 11:19
Show Gist options
  • Save eleev/d3541c4a4afb0b4bc9fbf7f5e1409234 to your computer and use it in GitHub Desktop.
Save eleev/d3541c4a4afb0b4bc9fbf7f5e1409234 to your computer and use it in GitHub Desktop.
This extnesion allows to get UIInterfaceOrientation from UIScreen. It is useful for cases when by some reasons UIStatusBar and UIDevice.current.orientation are not available e.g. iMessage extension target in iOS 10.
extension UIScreen {
var orientation: UIInterfaceOrientation {
let point = coordinateSpace.convert(CGPoint.zero, to: fixedCoordinateSpace)
if point == CGPoint.zero {
return .portrait
} else if point.x != 0 && point.y != 0 {
return .portraitUpsideDown
} else if point.x == 0 && point.y != 0 {
return .landscapeLeft
} else if point.x != 0 && point.y == 0 {
return .landscapeRight
} else {
return .unknown
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment