Skip to content

Instantly share code, notes, and snippets.

@vmanot
Created July 7, 2019 09:55
Show Gist options
  • Save vmanot/8b23f01a8afeb7634705c1a73f137a28 to your computer and use it in GitHub Desktop.
Save vmanot/8b23f01a8afeb7634705c1a73f137a28 to your computer and use it in GitHub Desktop.
PresentationLink bug workaround for SwiftUI (Xcode b3)
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