0

The following sample code illustrates an issue I am having with my SwiftUI app:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Hello, world!")
                .toolbar {
                    ToolbarItem {
                        Button {
                            print("house")
                        } label: { Image(systemName: "house") }
                    }
                    
                    ToolbarItem {
                        Button {
                            print("bookmark")
                        } label: { Image(systemName: "bookmark") }
                    }
                }
        } //NavigationView
    } //body
}

When I run this on an iPhone 13 simulator running iOS 16+, it works fine. But, on an iPhone 13 simulator running iOS 15.0, only the first toolbar button appears -- the "bookmark" button does not appear. I would appreciate any suggestions as to what I'm missing!

protasm
  • 1,209
  • 12
  • 20

2 Answers2

0

Solved by replacing ToolbarItems with HStack:

        .toolbar {
            HStack {
                Button {
                    print("house")
                } label: { Image(systemName: "house") }
                
                Button {
                    print("bookmark")
                } label: { Image(systemName: "bookmark") }
            } //HStack
        }
protasm
  • 1,209
  • 12
  • 20
0

I'd suggest using a ToolbarItemGroup for this, eg.

      .toolbar {
                ToolbarItemGroup(placement: .primaryAction){
                    Button {
                        print("house")
                    } label: { Image(systemName: "house") }
                    
                    Button {
                        print("bookmark")
                    } label: { Image(systemName: "bookmark") }
                }
            }

Although your HStack workaround will work, it might be more brittle to platform-specific changes, and the (optional) placement parameter lets you be explicit about where you want the buttons to appear.

Roo
  • 71
  • 3
  • I'm finding that this is still only showing one button (the first button) on iPhone 13 simulator running iOS 15.0 -- are you having a different result on that platform? – protasm Jun 24 '23 at 13:39