This gist accompanies a blog post on Masilotti.com, How to manage multiple sheets in SwiftUI.
Forked from joemasilotti/Multiple sheets in SwiftUI.md
Created
October 10, 2020 19:48
-
-
Save tazmancoder/bf1a7ded1f7f0d8b5f246dad6c620b04 to your computer and use it in GitHub Desktop.
Multiple sheets in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingsSheet: SheetState<SettingsSheet.State> { | |
enum State { | |
case attributions | |
case email | |
case feedback | |
case instructions | |
case licenseAgreement | |
case privacyPolicy | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SettingsView: View { | |
@ObservedObject var sheet = SettingsSheet() | |
var body: some View { | |
VStack { | |
Button("Attributions") { self.sheet.state = .attributions } | |
Button("Email") { self.sheet.state = .email } | |
Button("Feedback") { self.sheet.state = .feedback } | |
Button("Instructions") { self.sheet.state = .instructions } | |
Button("License Agreement") { self.sheet.state = .licenseAgreement } | |
Button("Privacy Policy") { self.sheet.state = .privacyPolicy } | |
} | |
.sheet(isPresented: $sheet.isShowing, content: sheetContent) | |
} | |
@ViewBuilder | |
private func sheetContent() -> some View { | |
if sheet.state == .attributions { | |
AttributionsView() | |
} else if sheet.state == .email { | |
EmailView() | |
} else if sheet.state == .feedback { | |
FeedbackView() | |
} else if self.sheet.state == .instructions { | |
InstructionsView() | |
} else if self.sheet.state == .licenseAgreement { | |
WebView(url: Policy.licenseURL) | |
} else if self.sheet.state == .privacyPolicy { | |
WebView(url: Policy.privacyURL) | |
} else { | |
EmptyView() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
class SheetState<State>: ObservableObject { | |
@Published var isShowing = false | |
@Published var state: State? { | |
didSet { isShowing = state != nil } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment