I am creating a SwiftUI app and wanted to add a settings screen that would be displayed as a modal sheet when tapping on a gear icon. For some reason when tapping on the gear when the app is running on a device or the simulator, the sheet does not appear. However, it does appear on the Xcode Preview when I run it.
I have included the entire ContentView code below and highlighted the part with the button that is supposed to display the sheet. SettingsPage() is just the normal SwiftUI template that says Hello, World.
Thanks.
struct ContentView: View {
@StateObject var locationViewModel = LocationManager()
@State private var showMoreInfo: Bool = false
@State private var showingTopSpeed: Bool = false
@State var showingSettingsScreen: Bool = false
private var currentSpeed: String = "Your current speed is:"
private var topSpeed: String = " Your top speed is:"
var body: some View {
VStack {
HStack(alignment: .lastTextBaseline) {
GPSStrengthView(horizontalAccuracy: locationViewModel.userHorizontalAccuracy)
Spacer()
Button(action: {
withAnimation {
showingTopSpeed.toggle()
print("Toggling showing top speed")
}
}) {
Image(systemName: "speedometer")
.foregroundColor(showingTopSpeed ? .primary : .accentColor)
}
Button(action: {
withAnimation {
showMoreInfo.toggle()
}
}) {
Image(systemName: "filemenu.and.selection")
.renderingMode(.original)
}
//BUTTON IN QUESTION ####################################
Button(action: {
showingSettingsScreen = true
}) {
Image(systemName: "gear")
} .sheet(isPresented: $showingSettingsScreen, content: {
SettingsPage()
})
//############################################
}
.padding(.leading)
.padding(.trailing)
Spacer()
Text(showingTopSpeed ? currentSpeed : topSpeed)
SpeedView(speed: showingTopSpeed ? locationViewModel.userSpeed : locationViewModel.userTopSpeed)
if showMoreInfo {
Text(String(format: "With an accuracy of: +/- %.2f", locationViewModel.userSpeedAccuracy * 2.236936))
.animation(.easeInOut)
}
Spacer()
HStack {
Spacer()
CompassView()
.scaleEffect(scaleVar)
.frame(width: frameDimensions, height: frameDimensions)
}
.padding()
}
}
question from:
https://stackoverflow.com/questions/65830947/swiftui-app-not-displaying-sheet-on-button-tap 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…