Skip to content

Instantly share code, notes, and snippets.

@matthewmorek
Created July 12, 2023 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmorek/a90e224f915e9868b5f52e5b7ec59c10 to your computer and use it in GitHub Desktop.
Save matthewmorek/a90e224f915e9868b5f52e5b7ec59c10 to your computer and use it in GitHub Desktop.
Triggering sheet using buttons inside NavigationStack ToolbarItem
//
// SampleView.swift
// iOS
//
// Created by Matthew Morek on 12/07/2023.
//
import SwiftUI
struct SampleView: View {
@State private var showSheet: Bool = false
var body: some View {
NavigationStack {
VStack {
Text("Example")
}
.toolbar {
// this placement crashes Preview when triggering any sheet from anywhere
// comment out ToolbarItem below to see the difference
ToolbarItem(placement: .navigationBarTrailing) {
Button {
self.showSheet = true
} label: {
Image(systemName: "info.circle")
}
}
// this placement does not crash the Preview when triggering any sheets
ToolbarItem(placement: .status) {
Button {
self.showSheet = true
} label: {
Image(systemName: "info.circle")
}
}
}
}
.sheet(isPresented: $showSheet, content: {
Text("SheetView")
})
}
}
struct SampleView_Previews: PreviewProvider {
static var previews: some View {
SampleView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment