-
-
Save GerardRJ/ea963314e3a26e4b46371f216d471cdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment