簡體   English   中英

在 SwiftUI 上使用 NavigationView 創建自定義后退按鈕

[英]Creating a custom back button with NavigationView on SwiftUI

我正在嘗試在 SwiftUI 上創建一個自定義后退按鈕,但我不知道該怎么做。

想法是隱藏左上角提供 NavigationView 的“后退”按鈕,並制作一個具有相同功能的自定義按鈕。

struct AnadirDatosViewA: View {
    @Environment(\.presentationMode) var presentation
    
    var body: some View{
        NavigationView(){
            Color(red: 48 / 255, green: 49 / 255, blue: 54 / 255)
                .edgesIgnoringSafeArea(.all)
                .overlay(
                    VStack{
                        AnadirDatosExpB()
                        
                        HStack{
                            
                            NavigationLink(destination:NuevoExperimentoView()){
                                Text("Back") //HERE
                                
                                NavigationLink(destination:AnadirDatosExpA()){
                                    Text("Next")
                                        
                                }
                            }
                        }
                    }
                )
        }.navigationBarBackButtonHidden(true)
    }
}

現在我通過使用我想要的視圖 go go 作為目的地來“作弊”,但它的工作方式不同......

我能做什么?

您可以在 Button 內的環境中使用presentationMode變量:

有關可能的實現,請參見下面的注釋示例。

struct ContentView: View{
    
    var body: some View{
        // I am navigating with a Navigationlink, so there is
        // no need for it in the AnadirDatosViewA
        NavigationView {
            NavigationLink("show AnadirDatosViewA") {
                AnadirDatosViewA()
            }
        }
    }
}


struct AnadirDatosViewA: View {
    @Environment(\.presentationMode) var presentation
    
    var body: some View{
        // if you navigated to this by a Navigationlink remove the NavigationView
        Color(red: 48 / 255, green: 49 / 255, blue: 54 / 255)
            .edgesIgnoringSafeArea(.all)
            .overlay(
                HStack{
                    // This Button will dismiss the View
                    Button("Back"){
                        // with help of th presentationMode from the environment
                        presentation.wrappedValue.dismiss()
                    }
                    // This NavigationLink can forward you to another view
                    NavigationLink("Next") {
                        TextView(text: "last")
                    }
                }
                // This will hide the back Button in this View
            ).navigationBarBackButtonHidden(true)
    }
}
// HelperView
struct TextView: View{
    var text: String
    var body: some View{
        Text(text)
    }
}

似乎 presentationMode 將來會被棄用,所以你也可以這樣做

    @Environment(\.dismiss) var dismiss

配對

Button(action: {
                dismiss()
            }, label: {
                Image(systemName: "chevron.left")
                Text(bookClub.bookTitle)
                    .fontWeight(.bold)
            })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM