Following the coding intro to SwiftUI a curiosity arose with regard to the toolbar placement in the view.
The first question is regarding the use of the toolbar modifier. The demo showed the buttons appearing at the top of the view, but they appear at the bottom of the view in my rendition, despite applying the modifiers to different blocks within the body calculated property. Any ideas as to why this would be would be appreciated.
Additionally, the question begs as to whether the .navigationBarItems modifier is to be replaced by the new toolbar modifier? Or, will it simply be added to the toolset?
The first question is regarding the use of the toolbar modifier. The demo showed the buttons appearing at the top of the view, but they appear at the bottom of the view in my rendition, despite applying the modifiers to different blocks within the body calculated property. Any ideas as to why this would be would be appreciated.
Additionally, the question begs as to whether the .navigationBarItems modifier is to be replaced by the new toolbar modifier? Or, will it simply be added to the toolset?
Code Block var body: some View { NavigationView { List { ForEach(store.sandwiches) { sandwich in SandwichCell(sandwich: sandwich) } .onMove(perform: moveSandwiches) .onDelete(perform: deleteSandwiches) HStack { Spacer() Text("\(store.sandwiches.count) Sandwiches") .foregroundColor(.secondary) Spacer() } } .navigationTitle("Sandwiches") .toolbar { #if os(iOS) EditButton() #endif Button("Add", action: makeSandwich) } Text("Select a sandwich") .font(.largeTitle) } }
You can wrap your button in a ToolbarItem, and explicitly specify the placement, like this:
You can see all of the values of the ToolbarItemPlacement in the documentation.
As for .navigationBarItems, I'm pretty sure this modifier is not supported on macOS natively, only if you make a Catalyst app. If you want a cross-platform view, you should rather opt for .toolbar.
Code Block .toolbar { ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) { Button("Add", action: makeSandwich) } }
You can see all of the values of the ToolbarItemPlacement in the documentation.
As for .navigationBarItems, I'm pretty sure this modifier is not supported on macOS natively, only if you make a Catalyst app. If you want a cross-platform view, you should rather opt for .toolbar.