Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/diniska/modal-view
Present Modal view with Swift UI in the same way you would do with NavigationView
https://github.com/diniska/modal-view
Last synced: 2 months ago
JSON representation
Present Modal view with Swift UI in the same way you would do with NavigationView
- Host: GitHub
- URL: https://github.com/diniska/modal-view
- Owner: diniska
- License: mit
- Created: 2019-09-25T22:01:26.000Z (over 5 years ago)
- Default Branch: void
- Last Pushed: 2021-10-09T13:12:02.000Z (over 3 years ago)
- Last Synced: 2024-10-30T04:39:36.106Z (3 months ago)
- Language: Swift
- Homepage:
- Size: 282 KB
- Stars: 92
- Watchers: 1
- Forks: 9
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- fucking-about-SwiftUI - Modal View
README
# ModalView
![Swift 5.1](https://img.shields.io/badge/Swift-5.1-FA5B2C) ![Xcode 11](https://img.shields.io/badge/Xcode-11-44B3F6) ![iOS 13.0](https://img.shields.io/badge/iOS-13.0-178DF6) ![iPadOS 13.0](https://img.shields.io/badge/iPadOS-13.0-178DF6) ![MacOS 10.15](https://img.shields.io/badge/MacOS-10.15-178DF6) ![Tests](https://github.com/diniska/modal-view/workflows/Tests%20runner/badge.svg)
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](https://github.com/diniska/modal-view)#### Step 2
Import the dependency```swift
import ModalView
```### Step 3
Use `ModalPresenter` and `ModalLink` the same way you would use `NavigationView` and `NavigationLink`:```swift
struct ContentView: View {
var body: some View {
ModalPresenter {
ModalLink(destination: Text("Modal View")) {
Text("Main view")
}
}
}
}
```### Result
![Presenting modal view with SwiftUI](./Docs/Resources/displaying-modal-view.gif)## Additional information
To add a "close" button to a modal view we can use a `dismiss` closure provided by the `ModalLink`:```swift
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.
```swift
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](https://medium.com/@diniska/modal-view-in-swiftui-3f9faf910249)