LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED: Spacing toolbar elements

Forums > SwiftUI

I'm trying to lay out a bottomBar toolbar, but this is the only way I've been able to do it that spaces the elements evenly:

        .toolbar {
            ToolbarItem(placement: .bottomBar) {
                Spacer()
            }
            ToolbarItem(placement: .bottomBar) {
                button1
            }
            ToolbarItem(placement: .bottomBar) {
                Spacer()
            }
            ToolbarItem(placement: .bottomBar) {
                button2
            }
            ToolbarItem(placement: .bottomBar) {
                Spacer()
            }
            ToolbarItem(placement: .bottomBar) {
                button3
            }
            ToolbarItem(placement: .bottomBar) {
                Spacer()
            }
        }

Every other way (like below) that I've tried results in all the buttons clustered together in the center, with huge amounts of empty space on either side (on iPad, I'm sure it looks better on a phone but I'm not targeting phones.)

        .toolbar {
            ToolbarItem(placement: .bottomBar) {
                HStack {
                    Spacer()
                    button1
                    Spacer()
                    button2
                    Spacer()
                    button3
                    Spacer()
                }
            }

The first way seems like a really clumsy way of accomplishing what I'm trying to do.

Is there some trick to spacing out toolbar items evenly that I'm missing?

   

At the moment Spacer() is not supported with the bottom tool bar. so there is no way to space the buttons out (finger crossed) maybe at WWDC and iOS 15

   

You can use ToolbarItemGroup to do this.

For example:

.toolbar {
  ToolbarItemGroup(placement: .bottomBar) {
    button1
    Spacer()
    button2
    Spacer()
    button3
  }
}

   

Thank you @nj-swiftdev. That is what I was looking for.

   

Click here to save 50% on all my 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.