SwiftUINavigator

0.3.1

insub4067/SwiftUINavigator

What's New

0.3.1

2023-10-15T05:41:50Z

🧭 SwiftUINavigator

License: MIT Static Badge Static Badge

Use SwiftUI View with UIKit Project easily.

🤔 Why do you need SwiftUINavigator?

With UIHostingController, I faced bugs that were hard to predict and could not be resolved. That's why I made SwiftUINavigator to solve those problems.

⚒️ How it works

스크린샷 2023-10-15 오후 2 20 50

✔️ Simple Example

let viewController = SwiftUIView() // View
    .asViewController() // WrapperViewController
    .title("SwiftUIView") // UIViewController
    .backgroundColor(.gray) // UIViewController
    .hidesBottomBarWhenPushed(true) // UIViewController

✔️ Project Example

UIKit side

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let scene = (scene as? UIWindowScene) else { return }
    window = UIWindow(frame: scene.coordinateSpace.bounds)
    window?.windowScene = scene
    
    let controller = RootView().asViewController()
    let navigationController = UINavigationController(rootViewController: controller)
    
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
}

SwiftUI side

struct RootView: Wrappable {
    
    weak var navigator: Navigator?
    
    var body: some View {
        Button("Button") {
            let controller = DestinationView().asViewController()
            navigator?.push(controller)
        }
    }
}

struct DestinationView: Wrappable {
    
    weak var navigator: Navigator?
    
    var body: some View {
        Button("Button") {
            navigator?.pop()
        }
    }
}

Description

  • Swift Tools 5.4.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Dec 08 2024 06:19:26 GMT-1000 (Hawaii-Aleutian Standard Time)