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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.