UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: .toolbar - Not working from App Template

Forums > SwiftUI

I created a brand new project (Type App / Use Core Data / SwiftUI / Swift Language). No changes from the template that XCode creates.

Run it (on iOS device and on iOS simulator)... no way to add a new item, no toolbar shows up at the top or bottom. Screen is blank. The template code is in the .toolbar.

.toolbar {
            #if os(iOS)
            EditButton()
            #endif

            Button(action: addItem) {
                Label("Add Item", systemImage: "plus")
            }
        }

2      

Have you wrapped the view content in a NavigationView? The .toolbar needs a navigation bar to show up.

Also you have (if iOS) two button try for the same space! You might want to add

.toolbar {
  ToolbarItem(placement: .navigationBarLeading) {
    #if os(iOS)
    EditButton()
    #endif
  }

  ToolbarItem(placement: .navigationBarTrailing) {
    Button(action: addItem) {
        Label("Add Item", systemImage: "plus")
    }
  }
}

4      

Yes, putting it in a navigation view and changing the .toolbar to your code works. I don't understand why Apple would put out a template that doesn't work at all (I've seen it before, just baffles me).

Thanks Nigel!

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.