Swiftpack.co - diniska/modal-view as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
diniska/modal-view
Present Modal view with Swift UI in the same way you would do with NavigationView
.package(url: "https://github.com/diniska/modal-view.git", from: "1.0.1")

ModalView

Swift 5.1 Xcode 11 iOS 13.0 iPadOS 13.0 MacOS 10.15 Tests

An analogue of SwiftUI NavigationView that provides a convenient interface of displaying modal views.

How to use

Step 1

Add a dependency using Swift Package Manager to your project: https://github.com/diniska/modal-view

Step 2

Import the dependency

import ModalView

Step 3

Use ModalPresenter and ModalLink the same way you would use NavigationView and NavigationLink:

struct ContentView: View {
    var body: some View {
        ModalPresenter {
            ModalLink(destination: Text("Modal View")) {
                Text("Main view")
            }
        }
    }
}

Result

Presenting modal view with SwiftUI

Additional information

To add a "close" button to a modal view we can use a dismiss closure provided by the ModalLink:

struct ContentView: View {
    var body: some View {
        ModalPresenter {
            ModalLink(destination: { dismiss in
                Button(action: dismiss) {
                    Text("Dismiss")
                }
            }) {
                Text("Main view")
            }
        }
    }
}

Moving the destination in the code above to a separate structure is a recommended way here to refactor the code here as modal views regularly contains a bit more that just a text or button.

struct ContentView: View {
    var body: some View {
        ModalPresenter {
            ModalLink(destination: MyModalView.init(dismiss:)) {
                Text("Main view")
            }
        }
    }
}

struct MyModalView: View {
    var dismiss: () -> ()
    var body: some View {
        Button(action: dismiss) {
            Text("Dismiss")
        }
    }
}

Learn more here: Display Modal View with SwiftUI

GitHub

link
Stars: 76
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Release 1.0.1
1 week ago

Xcode 13 Compatibility update

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics