SwiftUI 双导航栏 [英] SwiftUI double navigation bar

查看:35
本文介绍了SwiftUI 双导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SwiftUI 中遇到导航问题.我在导航栏上有一个按钮,如果点击它会推送一个带有项目列表的新导航视图.当点击这些项目之一时,它会推送详细信息视图.

I am having trouble with navigation in SwiftUI. I have a button on a navigation bar, if clicked it pushes a new navigation view with list of items. When one of those items is tapped, it pushes a detail view.

但我最终得到了这样的东西.

But I am ending up with something like this.

下面是代码

struct FirstView: View {

   var body: some View {
       NavigationView {
           List {
            ...
           }
           .navigationBarTitle(Text("First View"))
           .navigationBarItems(trailing: MyButton())
       }
    }
}

struct MyButton: View {
    var body: some View {
        NavigationLink("SecondView", destination: SecondView())
    }
}

struct SecondView: View {
   var body: some View {
       NavigationView {
           Text("My View")
       }
   }
}

推荐答案

SecondView 中移除 NavigationView.

NavigationLink 将第二个视图放在第一个视图导航视图中,因此您无需将其放在第二个视图中.

The NavigationLink puts the second view inside the first views navigations view, so you do not need to put it inside a second one.

您仍然可以像这样从 SecondView 更新视图的标题:

You can still update the title of the view from SecondView like so:

struct SecondView: View {
   var body: some View {
       Text("My View")
       .navigationBarTitle("Second View")
   }
}

这篇关于SwiftUI 双导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆