Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

ios - SwiftUI App not displaying sheet on button tap

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...