GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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?

3      

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

3      

You can use ToolbarItemGroup to do this.

For example:

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

4      

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

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

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.