Here is the default code of the SwiftUI project template in XCode 12.0.1
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.
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
that it would be visible.
Any leads as to why the automatic positioning is hiding the Add Item button?
Code Block swift 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.
Code Block swift 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
Code Block ToolbarItem(placement: .principal)
that it would be visible.
Any leads as to why the automatic positioning is hiding the Add Item button?