微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在导航栏中显示不同标题的问题

如何解决在导航栏中显示不同标题的问题

在我的代码中,我有这个:

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)
}

我希望,根据ForEach中按下的按钮,下一个视图的标题是按钮的名称。现在,每个按钮上显示名称是“ Buscar” 我试过在ForEach之后和NavigationLink之前实现NavigationView,但是ForEach漏洞消失了。基本上,我希望所按下的Text(specialty.type)是导航栏后退按钮的名称。有什么想法吗?

解决方法

这是一个可能方法的演示-想法是在激活导航链接时更改导航栏标题,然后在背面重置。

使用Xcode 12 / iOS 14准备并测试了演示

demo

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"
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。