Last active
July 26, 2020 04:04
-
-
Save jimmyhoran/8232a089c80a47ea55ecafb27d4f4aa1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension UIViewController { | |
/// Traverse up the responder chain until we find a `UINavigationController`. | |
func findNavigationController() -> UINavigationController? { | |
if let nextResponder = self.next as? UINavigationController { | |
return nextResponder | |
} else if let nextResponder = self.next as? UIViewController { | |
return nextResponder.findNavigationController() | |
} else { | |
return nil | |
} | |
} | |
} | |
extension UIView { | |
/// Traverse up the responder chain until we find a `UINavigationController`. | |
func findNavigationController() -> UINavigationController? { | |
if let nextResponder = self.next as? UINavigationController { | |
return nextResponder | |
} else if let nextResponder = self.next as? UIViewController { | |
return nextResponder.findNavigationController() | |
} else if let nextResponder = self.next as? UIView { | |
return nextResponder.findViewController()?.findNavigationController() | |
} else { | |
return nil | |
} | |
} | |
/// Traverse up the responder chain until we find a `UIViewController`. | |
func findViewController() -> UIViewController? { | |
if let nextResponder = self.next as? UIViewController { | |
return nextResponder | |
} else if let nextResponder = self.next as? UIView { | |
return nextResponder.findViewController() | |
} else { | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment