我正在尝试在当前用户的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代码的位置移到无济于事。
如何添加NavigationBarItems而不影响Picker Items的结构?