problems in displaying different titles in navigation bar

这一生的挚爱 提交于 2021-01-29 11:31:32

问题


in my code I have this:

var body: some View {
   NavigationView {
        VStack(spacing: 0) {
           
        
            ScrollView(.vertical, showsIndicators: false) {
            VStack(spacing: 18) {
                ScrollView(.horizontal, showsIndicators: false){
                    HStack(spacing: 20){
                        Text("teste")
                        .frame(height: 180)
                        .frame(width: 330)
                        .background(Color.blue)
                       .cornerRadius(15)
                        Text("teste")
                        .frame(height: 180)
                        .frame(width: 330)
                        .background(Color.green)
                        .cornerRadius(15)
                        Text("teste")
                        .frame(height: 180)
                        .frame(width: 330)
                        .background(Color.pink)
                        .cornerRadius(15)
                     
                    }.padding(.horizontal, 12)
                }
                ForEach(specialtyList, id: \.type){ specialty in
                    
                    NavigationLink (destination: SearchBar(item: specialty)){
                        VStack(spacing: 18) {
            HStack{
                
                Text(specialty.type).foregroundColor(.white)
                specialty.image
                .renderingMode(.original)
                    .resizable()
                    .frame(width: 35, height: 35)
                  
                
               }.frame(minWidth: 0, idealWidth: 350, maxWidth: .infinity)
                .frame(height: 100)
                        }
                  
                    }.padding(.horizontal)
                        .background(specialty.color).cornerRadius(45)
                   
            
                }.padding(.horizontal)
            }
           
            .padding(.top)
            .padding(.bottom)
                                        
            }
            
    }.navigationBarTitle("Buscar")
            
   
        
   }.accentColor(.black)
}

I want that, according to the button that is pressed in ForEach, the tittle of the next view is the name of the button. Now, the name displaying in every button is "Buscar" I've tried to implement, after ForEach and before NavigationLink, a NavigationView, but the hole ForEach disappears. Basically, I want that the Text(specialty.type) that is pressed is the name of the navigation bar back button. Any ideas?


回答1:


Here is a demo of possible approach - the idea is to change navigation bar title on the navigation link activation and reset on back.

Demo prepared & tested with Xcode 12 / iOS 14

struct DemoView: View {
    @State private var barTitle = ""
    @State private var selectedType = ""
    @State private var isActive = false

    let types = ["Type1", "Type2", "Type3"]

    var body: some View {
        NavigationView {
            VStack {
                ForEach(types, id: \.self) { type in
                    Button(type) {
                        self.isActive = true
                        self.selectedType = type
                        self.barTitle = type         // comment for variant 2
                    }
                }
            }
            .background(
                NavigationLink(destination:
                    Text(selectedType)
//                        .onAppear { self.barTitle = self.selectedType } // variant 2
                    , isActive: $isActive) { EmptyView() }
            )
            .navigationBarTitle(barTitle)
            .onAppear {
                self.barTitle = "Buscar"
            }
        }
    }
}


来源:https://stackoverflow.com/questions/63634075/problems-in-displaying-different-titles-in-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!