【问题标题】:How can I add section footer text below SwiftUI Picker options?如何在 SwiftUI Picker 选项下方添加节页脚文本?
【发布时间】:2020-01-21 01:07:14
【问题描述】:

我正在编写一个在 SwiftUI 上的应用程序,我使用 Picker 显示一个选项列表供用户选择。

当有人点击选择器并看到选项时,我想添加一些页脚文本来解释列表中的选项。

这是我想要完成的示例屏幕截图,取自 iOS 设置应用程序:

如何使用 SwiftUI 实现这一目标?

我在主屏幕上为包含选取器的部分定义了一个页脚(“这里是设置的简短描述。”)并且工作正常,但类似的代码不会在屏幕上添加一个页脚来显示选择器选项。

这是我的示例代码:

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

此外,当这个列表加载时,我看到了一个奇怪的跳跃。我怎样才能避免这种情况?

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

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

    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 的标签参数自动发生,但我特别想知道如何在选项下添加更多文本。
    • 空投不是picker。它来自一个导航链接
    • 您能举例说明如何使用 NavigationLink 复制它吗?
    • 谢谢!这正是我所需要的。为了复制 Picker 行为,我还将当前选择添加到 foregroundColor(.secondary)NavigationLink 的右侧,并将 ButtonforegroundColor 更改为 .primary
    • 感谢您的回答,我解决了看似无关的problem。为什么要两次定义标题“主屏幕”? (没有它,选择器会遇到我上面链接的问题)。
    猜你喜欢
    • 2021-12-10
    • 2020-01-14
    • 1970-01-01
    • 2013-11-14
    • 2020-07-16
    • 2022-10-05
    • 1970-01-01
    • 2020-09-03
    • 2021-12-12
    相关资源
    最近更新 更多