我该如何在SwiftUI Picker选项下添加章节页脚文本?

13

我正在使用SwiftUI编写一个应用程序,并使用Picker来呈现用户选择的选项列表。

当有人点击Picker并查看选项时,我想添加一些页脚文本来解释列表中的选择。以下是我想要实现的示例截图,取自iOS设置应用程序:AirDrop settings

如何使用SwiftUI实现这一点?

我在主屏幕上包含Picker的Section中定义了页脚(“这是设置的简短描述。”),这很好用,但是类似的代码却没有在显示Picker选项的屏幕上添加页脚。

这是我的示例代码:

import SwiftUI

class ViewModel: ObservableObject {

    enum Option: String, Identifiable, CaseIterable, CustomStringConvertible {
        case optionOne
        case optionTwo
        case optionThree

        var id: Option {
            self
        }

        var description: String {
            rawValue.prefix(1).uppercased() + rawValue.dropFirst()
        }
    }

    @Published var selectedOption: Option = .optionOne {
        didSet {
            print("new option selected: \(selectedOption.description)")
        }
    }
}

struct ContentView: View {

    @ObservedObject var viewModel = ViewModel()

    var body: some View {

        NavigationView {
            Form {
                Section(footer: Text("Here is a short description of the setting.")) {
                    Picker(selection: $viewModel.selectedOption,
                           label: Text("Choice")) {
                            Section(footer: Text("This is some additional text to help the user understand the options.")) {
                                ForEach(ViewModel.Option.allCases) { type in
                                    Text(type.description)
                                }
                            }
                    }
                }

            }.navigationBarTitle(Text("Main Screen"))
        }
    }
}

还有,当这个列表加载时,我看到了一个奇怪的跳动。我该如何避免这种情况?

1个回答

12

也许你不需要另一个双重页脚,只需要一个提示标题

struct ContentView: View {

    @ObservedObject var viewModel = ViewModel()

    var body: some View {

        NavigationView {
            Form {
                Section(footer: Text("Here is a short description of the setting.")) {
                    Picker(selection: $viewModel.selectedOption,
                           label: Text("Choice")) {

                            ForEach(ViewModel.Option.allCases) { type in
                                Text(type.description)
                            }.navigationBarTitle("hint title here", displayMode: .inline)

                    }
                }.navigationBarTitle("Main Screen", displayMode: .inline)

            }.navigationBarTitle(Text("Main Screen"))
        }
    }
}

没有使用选择器,您可以构建自己的选项列表:

struct ContentView: View {

    @ObservedObject var viewModel = ViewModel()

    var body: some View {

        NavigationView {
            Form {
                Section(footer: Text("Here is a short description of the setting.")) {
                    NavigationLink(destination: SelectionItemView(selection: $viewModel.selectedOption)) {
                        Text("Choice")
                    }


                }.navigationBarTitle("Main Screen", displayMode: .inline)

            }.navigationBarTitle(Text("Main Screen"))
        }
    }
}


struct SelectionItemView: View {

    @Binding var selection: ViewModel.Option
    var body: some View{
        Form{
            Section(footer: Text("Here is a detailed description of the setting.")) {
                ForEach(0 ..< ViewModel.Option.allCases.count, id: \.self) { index in

                    HStack{
                        Button(action: {
                            self.selection  = ViewModel.Option.allCases[index]
                        }){Text(ViewModel.Option.allCases[index].description)}
                        Spacer()
                        if ( self.selection  ==  ViewModel.Option.allCases[index] ){
                            Image(systemName: "checkmark")
                        }
                    }

                }
            }}


    }
}

我知道我可以更改标题,这已经基于Picker的标签参数自动发生,但我特别想知道如何在选项下添加更多文本。 - gohnjanotis
空投功能不是选择器,而是来自导航链接。 - E.Coms
你能展示一下如何使用NavigationLink复制那个的例子吗? - gohnjanotis
1
谢谢!这正是我所需要的。为了复制Picker的行为,我还使用foregroundColor(.secondary)将当前选择添加到了NavigationLink的右侧,并将ButtonforegroundColor更改为.primary - gohnjanotis
感谢您的回答,我解决了一个看起来不相关的问题(https://dev59.com/Pbzpa4cB1Zd3GeqPFSUW)。为什么要两次定义标题“主屏幕”?(如果没有这个,选择器会遇到上面链接中提到的问题)。 - Abhijit Sarkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接