添加presentationMode后SwiftUI .navigationBarItems .sheet崩溃

发布于 2025-01-14 08:38:45 字数 6192 浏览 2 评论 0原文

我创建了一个 ContentView() 页面,显示应用程序的主要内容。在添加 @Environment(\.presentationMode) varpresentationMode 和与presentationMode相关的函数之前,尾随部分的工作表视图可以正常运行,没有任何问题。现在,主页按钮(前导)运行良好,允许我返回应用程序的菜单页面,但单击另一个按钮(尾随)会立即使应用程序崩溃。奇怪的是 ContentView() 本身在预览模式下运行没有任何问题,但使模拟器崩溃。我尝试了很多方法,但找不到解决此问题的正确方法。

提前致谢。

这是 ContentView() 的部分:

import SwiftUI

struct ContentView: View {

@State var isAddPresented = false
@State var isActive : Bool = false
@Environment(\.presentationMode) var presentationMode
func goBack(){

        self.presentationMode.wrappedValue.dismiss()

    }
var body: some View {
    NavigationView{
            VStack{
                HStack{
                    Text("")
                        .navigationViewStyle(StackNavigationViewStyle())
                        .navigationBarItems(leading:
                                                Button(action: goBack) {
                                                Image("Home") },trailing:
                                                
                                                Button(action: {
                                                self.isAddPresented = true
                                                }) {
                                                Image("Rules_Click")
                                    
                                                            }).padding()
                                                }.sheet(isPresented: $isAddPresented,
                                                onDismiss: { self.isAddPresented = false })
                {RulesView()}.padding(.bottom, 100.0)
            }
    }

这是 HomeView() 的部分:

HStack{
            NavigationLink(destination: ContentView()){
                Image("NormalCards")
                    .resizable(resizingMode: .stretch)
                    .aspectRatio(contentMode: .fit)
                    .navigationTitle("")
                    .navigationBarTitleDisplayMode(.inline)
                    .navigationBarBackButtonHidden(true)
                    .navigationBarHidden(true)     
            }
                
        }

这是 RulesView 部分:

import SwiftUI

struct RulesView: View {
    var body: some View {
        Image("Rules")
            .resizable(resizingMode: .stretch)
            .aspectRatio(contentMode: .fill)
    }
}

struct Rules_Previews: PreviewProvider {
    static var previews: some View {
        RulesView()
    }
}

编辑:

将 .navigationBarItems 更改为工具栏没有改变任何内容。

                            .toolbar{
                            ToolbarItem(placement: .navigationBarLeading) {
                                    Button(action: {
                                        goBack()
                                    }, label: {
                                        Image("Home")
                                    })
                                }
                            ToolbarItem(placement: .navigationBarTrailing) {
                                    Button(action: {
                                        self.isAddPresented = true
                                    }, label: {
                                        Image("Rules_Click")
                                    }).sheet(isPresented: $isAddPresented, onDismiss: { self.isAddPresented = false }) {RulesView()}
                                }
                        }

编辑 Nr.2 :

这里使用 Text("ExampleText") 而不是 ZStack 效果很好。

struct ContentView: View {
    
    @Environment(\.dismiss) var dismiss
    @State private var isAddPresented = false
    
    func goBack(){
        dismiss() // reset to home view
    }
    
    var body: some View {
        
                ZStack(alignment:.center) {
                                ForEach(Card.data) { card in
                                    CardView(card: card)
                                }
                            }
        
        .navigationBarBackButtonHidden(true)
        
        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    goBack()
                }, label: {
                    Image("Home")
                })
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    self.isAddPresented = true
                }, label: {
                    Image("Rules_Click")
                })
            }
        }
        .sheet(isPresented: $isAddPresented) {
            RulesView()
        }
    }
}

在这里您可以找到有关 ZStack 的 CardView() 信息:

struct CardView: View {
    @State var card: Card
    var body: some View {
        ZStack {
            Image(card.imageName)
        }
        //.background(brownish)
        .offset(x: card.x, y: card.y)
        .rotationEffect(.init(degrees: card.deg))
        .gesture(
            DragGesture()
        
                .onChanged { value in
                    withAnimation(.default){
                        card.x = value.translation.width
                        card.y = value.translation.height
                        card.deg = 7*(value.translation.width > 0 ? 1 : -1)
                    }
                    
                }
                .onEnded { value in
                    withAnimation(.interpolatingSpring(mass: 1.0, stiffness: 50, damping: 8, initialVelocity: 0)) {
                        switch value.translation.width {
                            case 0...100:
                                card.x = 0; card.deg = 0; card.y = 0
                            case let x where x > 100:
                                card.x = 500; card.deg = 12
                            case (-100)...(-1):
                                card.x = 0; card.deg = 0; card.y = 0;
                            case let x where x < -100:
                                card.x = -500; card.deg = -12
                            default: card.x = 0; card.y = 0
                        }
                    }
                }
        )
    }
}


I have created a ContentView() page that shows the main content of the app. Before adding the @Environment(\.presentationMode) var presentationMode and the function concerning the presentationMode, the sheet view of the trailing part was functioning without any problems. Now the home button (leading) works well and allows me to return to the menu page of the app but clicking the other button (trailing) crashes the app immediately. The weird thing is that the ContentView() itself is functioning without any problems in the preview mode but crashes the simulator. I have tried many things but couldn't find the right way to solve this issue.

Thanks in advance.

This is the part from the ContentView():

import SwiftUI

struct ContentView: View {

@State var isAddPresented = false
@State var isActive : Bool = false
@Environment(\.presentationMode) var presentationMode
func goBack(){

        self.presentationMode.wrappedValue.dismiss()

    }
var body: some View {
    NavigationView{
            VStack{
                HStack{
                    Text("")
                        .navigationViewStyle(StackNavigationViewStyle())
                        .navigationBarItems(leading:
                                                Button(action: goBack) {
                                                Image("Home") },trailing:
                                                
                                                Button(action: {
                                                self.isAddPresented = true
                                                }) {
                                                Image("Rules_Click")
                                    
                                                            }).padding()
                                                }.sheet(isPresented: $isAddPresented,
                                                onDismiss: { self.isAddPresented = false })
                {RulesView()}.padding(.bottom, 100.0)
            }
    }

and this is the part from the HomeView():

HStack{
            NavigationLink(destination: ContentView()){
                Image("NormalCards")
                    .resizable(resizingMode: .stretch)
                    .aspectRatio(contentMode: .fit)
                    .navigationTitle("")
                    .navigationBarTitleDisplayMode(.inline)
                    .navigationBarBackButtonHidden(true)
                    .navigationBarHidden(true)     
            }
                
        }

And here is the RulesView part:

import SwiftUI

struct RulesView: View {
    var body: some View {
        Image("Rules")
            .resizable(resizingMode: .stretch)
            .aspectRatio(contentMode: .fill)
    }
}

struct Rules_Previews: PreviewProvider {
    static var previews: some View {
        RulesView()
    }
}

Edit:

Changing the .navigationBarItems to toolbar didn't change anything.

                            .toolbar{
                            ToolbarItem(placement: .navigationBarLeading) {
                                    Button(action: {
                                        goBack()
                                    }, label: {
                                        Image("Home")
                                    })
                                }
                            ToolbarItem(placement: .navigationBarTrailing) {
                                    Button(action: {
                                        self.isAddPresented = true
                                    }, label: {
                                        Image("Rules_Click")
                                    }).sheet(isPresented: $isAddPresented, onDismiss: { self.isAddPresented = false }) {RulesView()}
                                }
                        }

Edit Nr.2 :

Here using Text("ExampleText") instead of ZStack works just fine.

struct ContentView: View {
    
    @Environment(\.dismiss) var dismiss
    @State private var isAddPresented = false
    
    func goBack(){
        dismiss() // reset to home view
    }
    
    var body: some View {
        
                ZStack(alignment:.center) {
                                ForEach(Card.data) { card in
                                    CardView(card: card)
                                }
                            }
        
        .navigationBarBackButtonHidden(true)
        
        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    goBack()
                }, label: {
                    Image("Home")
                })
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    self.isAddPresented = true
                }, label: {
                    Image("Rules_Click")
                })
            }
        }
        .sheet(isPresented: $isAddPresented) {
            RulesView()
        }
    }
}

Here you can find the CardView() information regarding the ZStack:

struct CardView: View {
    @State var card: Card
    var body: some View {
        ZStack {
            Image(card.imageName)
        }
        //.background(brownish)
        .offset(x: card.x, y: card.y)
        .rotationEffect(.init(degrees: card.deg))
        .gesture(
            DragGesture()
        
                .onChanged { value in
                    withAnimation(.default){
                        card.x = value.translation.width
                        card.y = value.translation.height
                        card.deg = 7*(value.translation.width > 0 ? 1 : -1)
                    }
                    
                }
                .onEnded { value in
                    withAnimation(.interpolatingSpring(mass: 1.0, stiffness: 50, damping: 8, initialVelocity: 0)) {
                        switch value.translation.width {
                            case 0...100:
                                card.x = 0; card.deg = 0; card.y = 0
                            case let x where x > 100:
                                card.x = 500; card.deg = 12
                            case (-100)...(-1):
                                card.x = 0; card.deg = 0; card.y = 0;
                            case let x where x < -100:
                                card.x = -500; card.deg = -12
                            default: card.x = 0; card.y = 0
                        }
                    }
                }
        )
    }
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-01-21 08:38:45

主要是您应该将 .sheet.toolbar 中拉出。
其次,每个模式视图都有自己的dismiss,因此可能两者(NavigationLink 和 Sheet)会相互干扰。
最后你可以使用 @Environment(\.dismiss) var Dismiss 而不是 .presentationMode (它基本上是相同的东西,只是更短)

我重新排序了一点,来到了这个,这对我有用:

struct HomeView: View {

    var body: some View {
        NavigationView {
            VStack{
                NavigationLink {
                    ContentView()
                } label: {
                    Text("NormalCards")
                }
            }
            .navigationBarHidden(true)
        }
    }
}


struct ContentView: View {
    
    @Environment(\.dismiss) var dismiss
    @State private var isAddPresented = false
    
    func goBack(){
        dismiss() // reset to home view
    }
    
    var body: some View {
        
        VStack{
            HStack{
                Text("Example Text")
            }
        }
        .navigationBarBackButtonHidden(true)
        
        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    goBack()
                }, label: {
                    Text("Home")
                })
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    self.isAddPresented = true
                }, label: {
                    Text("Rules")
                })
            }
        }
        .sheet(isPresented: $isAddPresented) {
            RulesView()
        }
    }
}

struct RulesView: View {
    
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        NavigationView {
            Text("Here are the rules")

                .toolbar {
                    Button("Close") {
                        dismiss()
                    }
                }
        }
    }
}

Mainly you should pull the .sheet out of the .toolbar.
Secondly each modal view has its own dismiss, so maybe the two (NavigationLink and Sheet) interfere.
Lastly you can use @Environment(\.dismiss) var dismiss instead of .presentationMode (it is basically the same thing, just shorter)

I reordered a little and came to this, which works for me:

struct HomeView: View {

    var body: some View {
        NavigationView {
            VStack{
                NavigationLink {
                    ContentView()
                } label: {
                    Text("NormalCards")
                }
            }
            .navigationBarHidden(true)
        }
    }
}


struct ContentView: View {
    
    @Environment(\.dismiss) var dismiss
    @State private var isAddPresented = false
    
    func goBack(){
        dismiss() // reset to home view
    }
    
    var body: some View {
        
        VStack{
            HStack{
                Text("Example Text")
            }
        }
        .navigationBarBackButtonHidden(true)
        
        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    goBack()
                }, label: {
                    Text("Home")
                })
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    self.isAddPresented = true
                }, label: {
                    Text("Rules")
                })
            }
        }
        .sheet(isPresented: $isAddPresented) {
            RulesView()
        }
    }
}

struct RulesView: View {
    
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        NavigationView {
            Text("Here are the rules")

                .toolbar {
                    Button("Close") {
                        dismiss()
                    }
                }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文