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
193 views
in Technique[技术] by (71.8m points)

ios - SwiftUI StackNavigationViewStyle issue when rotating iPhone

I want to implement a Settings view which can be opened taping on a gear icon button in the navigation tool bar. This button opens a SwiftUI sheet with on Ok button to validate settings and close the settings window.

It works well if you use it without rotating the iPhone. But if you rotate the phone when the settings window is opened, the Ok button does not work anymore and the window stays on screen (even if you rotate back the phone).

In the console, an error appears when I rotate the phone. Here is the message:

[Presentation] Attempt to present <…> on <…> (from <…>) which is already presenting

This issue seems to be linked to StackNavigationViewStyle() modifier I use to not have 2 columns on landscape mode.

If I remove the following line, the bug disappears but the layout is no more the one I want. .navigationViewStyle(StackNavigationViewStyle())

Here is a sample code I wrote to reproduce the problem:

import SwiftUI

struct ContentView: View {
    // Size class
    @Environment(.verticalSizeClass) var sizeClassV
    
    @State private var showGearView: Bool = false
    
    
    var gearButton: some View {
        HStack {
            Button(action: {
                self.showGearView.toggle()
            }) {
                Image(systemName: "gear")
                    .imageScale(.large)
                    .accessibility(label: Text("Settings"))
            }
            .sheet(isPresented: self.$showGearView, onDismiss: {
            }, content: {
                gearView()
            })
        }
    }
    
    var body: some View {
        return NavigationView {
            //            if sizeClassV == .regular {
            VStack {
                Text("Click on Gear and rotate your iPhone: here is the bug when clicking on Ok: the sheet does not collapse!")
                    .multilineTextAlignment(.center)
                    .padding(.all)
            }
            .padding(.all)
            .navigationTitle("Bug")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar(content: { gearButton })
        }
        // The bug only happens when adding the StackNavigationViewStyle below
        .navigationViewStyle(StackNavigationViewStyle())
    }
}



struct gearView: View {
    @Environment(.presentationMode) var presentationMode
    
    var OKButton: some View {
        HStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("OK")
            }
        }
    }
    
    var body: some View {
        return NavigationView {
            VStack {
                Form {
                    Section(header: Text("Settings")) {
                        Text("No Settings")
                    }
                }
            }
            .navigationTitle(Text("Settings"))
            .toolbar(content: {
                ToolbarItem(placement: .primaryAction) {
                    OKButton
                }
            })
        }
    }
}


question from:https://stackoverflow.com/questions/66055770/swiftui-stacknavigationviewstyle-issue-when-rotating-iphone

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...