将NavigationBarItems添加到Picker时如何修复折叠列表

时间:2019-10-23 21:58:22

标签: swift swiftui swiftui-form

我正在尝试在当前用户的SwiftUI Picker列表中包括“ +”或“新用户”按钮,以允许用户将新用户添加到列表中。当我添加.navigationBarItems时,当前用户列表会折叠到一个表单项中。

import SwiftUI

struct SuperHeroView: View {
    private var users = [User(id: UUID(), name: "Batman", username: "brucewayne"),
                 User(id: UUID(), name: "Spider Man", username: "peterparker"),
                 User(id: UUID(), name: "Superman", username: "clarkkent")]
    @State private var selectedSuperHero = UUID()
    var body: some View {
        NavigationView {
            VStack {
                Form {
                    Picker(selection: $selectedSuperHero, label: Text("Super Hero")) {
                        ForEach(users) { user in
                            Text(user.name)
                        }
                    }
                }
            }
        }
    }
}

original

更改为:

import SwiftUI

struct SuperHeroView: View {
    private var users = [User(id: UUID(), name: "Batman", username: "brucewayne"),
                 User(id: UUID(), name: "Spider Man", username: "peterparker"),
                 User(id: UUID(), name: "Superman", username: "clarkkent")]
    @State private var selectedSuperHero = UUID()
    var body: some View {
        NavigationView {
            VStack {
                Form {
                    Picker(selection: $selectedSuperHero, label: Text("Super Hero")) {
                        ForEach(users) { user in
                            Text(user.name)
                        }.navigationBarItems(trailing: Text("New User"))
                    }
                }
            }
        }
    }
}

using navigationBarItems

我尝试将.navigationBarItems代码的位置移到无济于事。

如何添加NavigationBarItems而不影响Picker Items的结构?

0 个答案:

没有答案