Skip to content

Instantly share code, notes, and snippets.

@NunoAlexandre
Last active March 31, 2022 12:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save NunoAlexandre/a0cd5af9a5c1ea1933baf8a3c6b426f0 to your computer and use it in GitHub Desktop.
Save NunoAlexandre/a0cd5af9a5c1ea1933baf8a3c6b426f0 to your computer and use it in GitHub Desktop.
/// This module contains useful extensions to the base
/// navigation controllers in respect to managing orientation,
/// as well as adding missing functionality to the sdk types.
import Foundation
func currentOrientation() -> UIInterfaceOrientation {
return UIApplication.shared.statusBarOrientation
}
extension YourNavigationController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return visibleViewController!.supportedInterfaceOrientations
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return visibleViewController!.preferredInterfaceOrientationForPresentation
}
func adjustOrientationIfNeeded() {
if self.supportedInterfaceOrientations.misses(currentOrientation()) {
UIDevice.current
.setValue(self.preferredInterfaceOrientationForPresentation.rawValue,
forKey: "orientation")
}
}
}
extension UIInterfaceOrientationMask {
func supports(_ orientation: UIInterfaceOrientation) -> Bool {
return (orientation.isLandscape && self.contains(.landscape))
|| (orientation.isPortrait && self.contains(.portrait))
}
func misses(_ orientation: UIInterfaceOrientation) -> Bool {
return !supports(orientation)
}
public var description: String {
switch self {
case .all:
return "all"
case .allButUpsideDown:
return "allButUpsideDown"
case .landscape:
return "landscape"
case .landscapeLeft:
return "landscapeLeft"
case .landscapeRight:
return "landscapeRight"
case .portrait:
return "portrait"
case .portraitUpsideDown:
return "portraitUpsideDown"
default:
return "Unkown UIInterfaceOrientationMask"
}
}
}
extension UIInterfaceOrientation {
public var description: String {
switch self {
case .landscapeLeft:
return "landscapeLeft"
case .landscapeRight:
return "landscapeRight"
case .portrait:
return "portrait"
case .portraitUpsideDown:
return "portraitUpsideDown"
case .unknown:
return "unknown"
}
}
func toMask() -> UIInterfaceOrientationMask {
return self.isPortrait ? .portrait : .landscape
}
}
/// iOS v9.* has a bug that requires this extension, otherwise the app crashes
/// with the following message:
/// "UIAlertController:supportedInterfaceOrientations was invoked recursively"
/// Read more at https://stackoverflow.com/a/32010520/10070651.
extension UIAlertController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// By having the alert supporting only the current orientation, we make sure
// that we don't override the parent view controller supported orientations.
// This is fine for users too since they were already in this orientation.
return currentOrientation().toMask()
}
open override var shouldAutorotate: Bool {
return false
}
}
@GerardRJ
Copy link

Besides

import UIKit

The two line change below should be made so there is no compiler warnings. see https://www.avanderlee.com/swift/unknown-default-enums-in-swift/

extension UIInterfaceOrientation {
public var description: String {
switch self {
case .landscapeLeft:
return "landscapeLeft"
case .landscapeRight:
return "landscapeRight"
case .portrait:
return "portrait"
case .portraitUpsideDown:
return "portraitUpsideDown"
case .unknown:
fallthrough // <<< change
@unknown default: // <<< change
return "unknown"
}
}

func toMask() -> UIInterfaceOrientationMask {
return self.isPortrait ? .portrait : .landscape
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment