WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

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")
            }
        }

   

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")
    }
  }
}

1      

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!

2      

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.