Swiftui-如何设置子屏幕标题

发布于 2025-01-25 06:22:24 字数 1895 浏览 4 评论 0原文

我有一个Swiftui主屏幕:

import SwiftUI

struct HomeView: View {
    @State private var navigateToSettingsView : Bool = false
    
    var body: some View {
        NavigationView {
            if navigateToSettingsView {
                NavigationLink(destination: UserSettingsView(), isActive: $navigateToSettingsView) {
                    EmptyView()
                }
                .navigationTitle("Home") // This overrides the word Back with Home on the child back button
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        VStack {
                            Text("User Settings").font(.headline)
                        }
                    }
                }
            }
            else {
                NavigationLink(destination: Text("Hello, I am HomeView child screen")) {
                    homeScreen
                }
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button(action: {
                            navigateToSettingsView = true
                        }) {
                            Image(systemName: "gearshape")
                        }
                    }
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Text("App Name")
                    }
                }
            }
        }
    }
}

extension HomeView {
    private var homeScreen: some View {
        VStack {
            Text("Hello, I am HomeView")
        }
        .frame(maxWidth: .infinity)
    }
}

现在,Userettingsview是基本的:

import SwiftUI

struct UserSettingsView: View {
    var body: some View {
        VStack {
            Text("I am a UserSettingsView")
        }
    }
}

我正在努力的是在用户单击齿轮图标时设置子屏幕标题。工具栏似乎被忽略了。您如何设置子屏幕的标题,以便具有用户设置的标题?

I have a SwiftUI Home screen:

import SwiftUI

struct HomeView: View {
    @State private var navigateToSettingsView : Bool = false
    
    var body: some View {
        NavigationView {
            if navigateToSettingsView {
                NavigationLink(destination: UserSettingsView(), isActive: $navigateToSettingsView) {
                    EmptyView()
                }
                .navigationTitle("Home") // This overrides the word Back with Home on the child back button
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        VStack {
                            Text("User Settings").font(.headline)
                        }
                    }
                }
            }
            else {
                NavigationLink(destination: Text("Hello, I am HomeView child screen")) {
                    homeScreen
                }
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button(action: {
                            navigateToSettingsView = true
                        }) {
                            Image(systemName: "gearshape")
                        }
                    }
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Text("App Name")
                    }
                }
            }
        }
    }
}

extension HomeView {
    private var homeScreen: some View {
        VStack {
            Text("Hello, I am HomeView")
        }
        .frame(maxWidth: .infinity)
    }
}

The UserSettingsView is just basic right now:

import SwiftUI

struct UserSettingsView: View {
    var body: some View {
        VStack {
            Text("I am a UserSettingsView")
        }
    }
}

What I am struggling with is setting the title of the child screen when the user clicks on the Gear icon. The ToolbarItem seems to be ignored. How do you set the title of the child screen so that it has a title of User Settings?

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

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

发布评论

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

评论(1

沩ん囻菔务 2025-02-01 06:22:24

我相信您可以使用更清洁的方法来实现想要的目标。

下面的示例将按钮用另一个navigationLink替换。它维护home文本,而不是back,但它还在用户设置屏幕中显示标题。唯一的收获是标题位于顶部区域的中心,而不是领先位置。

请注意,@State变量不再存在。

struct Example: View {
    
    var body: some View {
        NavigationView {
                NavigationLink(destination: Text("Hello, I am HomeView child screen")) {
                    Text("I'm a home screen")
                }
                .navigationTitle("Home")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        NavigationLink(destination: UserSettingsView().navigationTitle("User settings")) {
                            Image(systemName: "gearshape")
                        }
                    }
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Text("App Name")
                    }
            }
        }
    }
}

I believe you can achieve what you want using a cleaner approach.

The example below replaces the Button with another NavigationLink. It maintains the Home text instead of Back, but it also shows a title in the user settings screen. The only catch is that the title is in the center of the top area, not in the leading position.

Note that the @State variable does not exist anymore.

struct Example: View {
    
    var body: some View {
        NavigationView {
                NavigationLink(destination: Text("Hello, I am HomeView child screen")) {
                    Text("I'm a home screen")
                }
                .navigationTitle("Home")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        NavigationLink(destination: UserSettingsView().navigationTitle("User settings")) {
                            Image(systemName: "gearshape")
                        }
                    }
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Text("App Name")
                    }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文