Created
July 12, 2023 19:52
-
-
Save matthewmorek/a90e224f915e9868b5f52e5b7ec59c10 to your computer and use it in GitHub Desktop.
Triggering sheet using buttons inside NavigationStack ToolbarItem
This file contains 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
// | |
// 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