Created
July 7, 2019 09:55
-
-
Save vmanot/8b23f01a8afeb7634705c1a73f137a28 to your computer and use it in GitHub Desktop.
PresentationLink bug workaround for SwiftUI (Xcode b3)
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
extension UIApplication { | |
/// The top most view controller | |
static var topMostViewController: UIViewController? { | |
return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController | |
} | |
} | |
extension UIViewController { | |
/// The visible view controller from a given view controller | |
var visibleViewController: UIViewController? { | |
if let navigationController = self as? UINavigationController { | |
return navigationController.topViewController?.visibleViewController | |
} else if let tabBarController = self as? UITabBarController { | |
return tabBarController.selectedViewController?.visibleViewController | |
} else if let presentedViewController = presentedViewController { | |
return presentedViewController.visibleViewController | |
} else { | |
return self | |
} | |
} | |
} | |
public struct PresentationLink2<Label: View, Destination: View>: View { | |
public let destination: Destination | |
let _label: () -> Label | |
public var label: Label { | |
return _label() | |
} | |
public init(destination: Destination, label: @escaping () -> Label) { | |
self.destination = destination | |
self._label = label | |
} | |
public var body: some View { | |
return _label().tapAction { | |
UIApplication.topMostViewController?.presentViewControllerFromVisibleViewController(UIHostingController(rootView: self.destination), animated: true, completion: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment