I'm trying out XCode 12 for the first time and chose the SwiftUI 2.0 sample application. I'm wondering why the Button inside the .toolbar modifier on the List of ContentView is not showing up on the preview.
Have you guys experienced this?
Post
Replies
Boosts
Views
Activity
Here is the default code of the SwiftUI project template in XCode 12.0.1
var body: some View {
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
EditButton()
#endif
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
By default, the Edit button and the Add Item button don't appear on the preview. I have to wrap the List in a NavigationView before the Edit button appears.
var body: some View {
NavigationView {
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
ToolbarItem(placement: .automatic) {
EditButton()
}
#endif
ToolbarItem(placement: .automatic) {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
.navigationTitle("Main List")
}
}
I've wrapped the buttons in ToolbarItems so that they can be positioned. At this point, the Add Item button is still not visible on the screen. It's only if I change it to something like
ToolbarItem(placement: .principal)
that it would be visible.
Any leads as to why the automatic positioning is hiding the Add Item button?