Skip to content

Instantly share code, notes, and snippets.

@kingiol
Forked from vmanot/PresentationLink2.swift
Created July 10, 2019 10:25
Show Gist options
  • Save kingiol/0f389469376e13239479f64a45875b74 to your computer and use it in GitHub Desktop.
Save kingiol/0f389469376e13239479f64a45875b74 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