简体   繁体   English

如何在 SwiftUI 的导航栏中添加类似“副标题”的内容?

[英]How can I add something like a "subheader" into the navigation bar in SwiftUI?

"My" solution: “我的”解决方案:

Thanks to the help of Midhun MP and Mojtaba Hosseini I found this solution.感谢Midhun MPMojtaba Hosseini的帮助,我找到了这个解决方案。 It works ok, but the translucent effect of the navigation bar does not work anymore.它工作正常,但导航栏的半透明效果不再起作用。 So, if anyone knows how to fix it, please let me know.所以,如果有人知道如何修复它,请告诉我。

// UITableView.appearance().backgroundColor = UIColor(named: "CustomTableViewBackgroundColor") // These are all custom color sets
// UITableViewCell.appearance().backgroundColor = UIColor(named: "CustomTableViewCellBackgroundColor")

// For the navigation bar color
UINavigationBar.appearance().backgroundColor = UIColor(named: "CustomNavigationBarBackgroundColor")
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

return VStack(spacing: 0) {
    // This is the "subheader"
    Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color("CustomNavigationBarBackgroundColor")) // This is also a custom color set
        .font(.footnote)
    // And here is my normal NavigationView
    NavigationView {
        List {
            Text("Hello")
        } .listStyle(GroupedListStyle())

        .navigationBarTitle(Text(""), displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(
            leading:
            Button("Cancel") {
                // self.presentationMode.wrappedValue.dismiss()
            }.padding(.vertical, 5),
            trailing:
            Button("Done") {

            }.padding(.vertical, 5).disabled(true)
        )
    }
}

在此处输入图像描述

My original question:我原来的问题:

I want to insert something like this in my navigation bar.我想在我的导航栏中插入这样的东西。 So if someone can help me that would be nice.因此,如果有人可以帮助我,那就太好了。

在此处输入图像描述

My code now我现在的代码

NavigationView {
    List {
        Text("Hello")
    } .listStyle(GroupedListStyle())

    .navigationBarTitle(Text(""), displayMode: .inline)
    .navigationBarBackButtonHidden(true)
    .navigationBarItems(
        leading:
        Button("Cancel") {
            self.presentationMode.wrappedValue.dismiss()
        },
        trailing:
        Button("Done") {

        }.disabled(true)
    )
}

And a photo of how my code looks compiled以及我的代码看起来如何编译的照片

在此处输入图像描述

You can just add a text just before the navigation view.您可以在导航视图之前添加一个文本。

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Sub Header")
            NavigationView {
                List {
                    Text("Hello")
                } .listStyle(GroupedListStyle())

                .navigationBarTitle(Text("Hello There"), displayMode: .inline)

                .navigationBarBackButtonHidden(true)
                .navigationBarItems(
                    leading:
                    Button("Cancel") {
                        //self.presentationMode.wrappedValue.dismiss()
                    },
                    trailing:
                    Button("Done") {

                    }.disabled(true)
                )
            }
        }
    }
}

Note: If you go to detail pages, the above sub-label will be still visible.注意:如果您将 go 转到详细信息页面,上述子标签仍将可见。 If you don't want that, You can create a custom header using normal view.如果您不想这样,您可以使用普通视图创建自定义 header。

Starting with iOS14 its possible to use .toolbar {...} :从 iOS14 开始,可以使用.toolbar {...}

副标题示例图片

NavigationView {
    Text("Hello, SwiftUI!")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .principal) {
                VStack {
                    Text("Title").font(.headline)
                    Text("Subtitle").font(.subheadline)
                }
            }
        }
}

Credits https://sarunw.com/posts/custom-navigation-bar-title-view-in-swiftui/积分https://sarunw.com/posts/custom-navigation-bar-title-view-in-swiftui/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM