Skip to content

Instantly share code, notes, and snippets.

@T-Pham
Created June 24, 2021 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T-Pham/0ed5a6ab2fdc9293e4071367dd64b658 to your computer and use it in GitHub Desktop.
Save T-Pham/0ed5a6ab2fdc9293e4071367dd64b658 to your computer and use it in GitHub Desktop.
Get UIInterfaceOrientation based on UIScreen's UICoordinateSpaces
extension UIScreen {
var orientation: UIInterfaceOrientation {
let convertedZeroPoint = fixedCoordinateSpace.convert(CGPoint.zero, to: coordinateSpace)
let coordinateSpaceBounds = coordinateSpace.bounds
switch convertedZeroPoint {
case .zero:
return .portrait
case CGPoint(x: coordinateSpaceBounds.maxX, y: coordinateSpaceBounds.minY):
return .landscapeLeft
case CGPoint(x: coordinateSpaceBounds.minX, y: coordinateSpaceBounds.maxY):
return .landscapeRight
case CGPoint(x: coordinateSpaceBounds.maxX, y: coordinateSpaceBounds.maxY):
return .portraitUpsideDown
default:
return .unknown
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment