我正在尝试在当前用户的 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)
}
}
}
}
}
}
}
更改为:
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"))
}
}
}
}
}
}
我试过移动 .navigationBarItems 代码的位置,但无济于事。
如何在不影响 Picker Items 结构的情况下添加 navigationBarItems?