A simple example of usage.
Code example:
import SwiftUI
import AppRouter /// Import package
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openProfileView(_ sender: UIButton) {
}
}import SwiftUI
import AppRouter /// Import package
struct ProfileView: View {
let router: AppRouter /// set here
var body: some View {
Text("This is Profile view with SwiftUI")
}
}
import SwiftUI
import AppRouter
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openProfileView(_ sender: UIButton) {
let hostView = UIHostingController(rootView: ProfileView(router: AppRouter(rootViewController: self)))
self.present(hostView, animated: true)
}
}import SwiftUI
class ProfileViewVC: UIHostingController<ProfileView> {
override func viewDidLoad() {
super.viewDidLoad()
}
}import SwiftUI
import AppRouter
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openProfileView(_ sender: UIButton) {
let vc = ProfileViewVC(rootView: ProfileView(router: AppRouter(rootViewController: self)))
if let sheet = vc.sheetPresentationController {
sheet.prefersGrabberVisible = true
sheet.detents = [.medium(), .large()]
sheet.preferredCornerRadius = 20
}
vc.hidesBottomBarWhenPushed = true
present(vc, animated: true)
}
}import SwiftUI
import AppRouter
struct ProfileView: View {
let router: AppRouter
var body: some View {
VStack(spacing: 24) {
Text("This is Profile view with SwiftUI")
.onTapGesture {
/// router.pushViewController(UIViewController()) set your viewcontroller here
}
HStack(spacing: 16) {
Button("Back to Home") {
router.dismissViewController()
}
Button("Create New rofile here") {
let vc = CreateNewProfileFormVC()
router.presentViewController(vc)
}
}
}
}
}import SwiftUI
/// A class responsible for navigating to different views within the app using the provided router.
public final class Navigator {
/// The router used for navigation.
let router: AppRouter
/// Initializes the `Navigator` with the provided router.
/// - Parameter router: The router used for navigation.
init(router: AppRouter) {
self.router = router
}
}
fun yourUIViewcontrollerName() {
let vc = YourViewcontrollerName()
vc.title = "This is UIKit UIViewcontroller"
router.pushViewController(vc)
} func YourViewControllerName() {
let vc = UIHostingController(rootView: SalonDetailView())
vc.hidesBottomBarWhenPushed = true
vc.title = title
router.pushViewController(vc)
}
} class YourViewControllerName: UIHostingController<SomeSwiftUIView> {
override func viewDidLoad() {
super.viewDidLoad()
}
}
func YourViewControllerName() {
let vc = YourViewControllerName(rootView: SomeSwiftUIView())
vc.hidesBottomBarWhenPushed = true
vc.title = title
router.pushViewController(vc)
}
}class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationController?.setNavigationBarHidden(isHide, animated: !isHide)
self.navigationController?.navigationBar.isHidden = isHide
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.setHostView(rootView: view, swiftUIView: SwiftUIViewHere(router: AppRouter(rootViewController: self)))
}
}extension UIViewController {
/// SwiftUI View to UIKit View
func setHostView<Content: View>(rootView: UIView, swiftUIView: Content) {
let hostView = UIHostingController(rootView: swiftUIView)
hostView.view.translatesAutoresizingMaskIntoConstraints = false
rootView.addSubview(hostView.view)
NSLayoutConstraint.activate([
hostView.view.topAnchor.constraint(equalTo: rootView.topAnchor),
hostView.view.leadingAnchor.constraint(equalTo: rootView.leadingAnchor),
hostView.view.trailingAnchor.constraint(equalTo: rootView.trailingAnchor),
hostView.view.bottomAnchor.constraint(equalTo: rootView.bottomAnchor),
hostView.view.widthAnchor.constraint(equalTo: rootView.widthAnchor),
hostView.view.heightAnchor.constraint(equalTo: rootView.heightAnchor)
])
}
}