How does one use SwiftUI's ToolbarItemGroup?

ToolbarItemGroup is designed to group views in the same toolbar. It removes the need for explicit usage of ToolbarItem, as both conform to ToolbarContent.

e.g.

.toolbar {
  ToolbarItemGroup {
    Button("200%",  action: zoom200)
      .foregroundColor(controller.scale == 2.0 ? selectedButtonColor : defaultButtonColor)
    Button("100%", action: zoom100)
      .foregroundColor(controller.scale == 1.0 ? selectedButtonColor : defaultButtonColor)
  }
  ToolbarItemGroup(placement: .bottomBar) {
    Spacer()
    Button("Open", action: open)
    Spacer()
    Button("Close", action: close)
    Spacer()
  }
}

It's also the only way I know of to get Spacers to work, between toolbar items.

Tags:

Swiftui