Prepare toolbar items for a vertical bar

⋅ 4 min read ⋅ iOS SwiftUI UIKit

Table of Contents

Part 2 in the series "Vertical bars on iPhone Duo", based on Apple's Raise the bar with iPhone Duo talk.

  1. Opt in to vertical bars on iPhone Duo
  2. Prepare toolbar items for a vertical bar
  3. Adapt custom toolbar views for iPhone Duo
  4. Toolbar overflow and visibility priority on iPhone Duo

In the previous article, we got a vertical bar to appear. Now let's look at the items inside it.

You don't move items into a vertical bar yourself. You keep declaring the same toolbar content you always have, and the system decides which items can go vertical. This article is about how that decision works and how to influence it.

Toolbar content

Toolbar content can contain two pieces of data:

  • Icon
  • Title

Most of the time we might set only one, but we should provide both from now on and let the system choose the representation based on the context.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Why two pieces of data really matter on iPhone Duo

The two axes have opposite constraints:

Bar Fixed Flexible
Horizontal Height Item width
Vertical Width Item height
A horizontal toolbar on a grid, with a fixed height and arrows showing that item widths can grow and shrink.
A horizontal bar has a fixed height and flexible item width. Image: Apple, Raise the bar with iPhone Duo, 6:07.
A vertical toolbar on a grid, with a fixed width and arrows showing that item heights can grow and shrink.
A vertical bar has a fixed width and flexible item height. Image: Apple, Raise the bar with iPhone Duo, 6:12.

A horizontal bar can let a button grow as wide as its label needs. A vertical bar can't, because its width is fixed. A long text label has nowhere to go.

That single fact explains most of the guidance here: vertical bars suit symbol-only items.

How the system picks a representation

In the top and bottom bars

  • an item prefers to show an icon.
  • If there's no icon, it shows the text.

In the overflow menu

  • it shows both.
Two iPhones: a toolbar with Edit as text and Share as an icon, beside an overflow menu that shows both the Edit title and the Share icon with title.
The overflow menu shows both the title and the icon. Image: Apple, Raise the bar with iPhone Duo, 6:48.

None of that changes with vertical bars. What's new is one extra question the system asks: is this content better suited to a vertical or a horizontal axis?

  • Items with an icon, like back and share, move to the vertical axis.
  • Text-only items, like Edit, stay horizontal.
A conventional iPhone and iPhone Duo side by side. Edit stays as a text button at the top on both, while back and share move to the vertical bar on iPhone Duo.
Text-only items stay on the horizontal axis. Image: Apple, Raise the bar with iPhone Duo, 7:18.

So if you're already setting both a title and an image, through a SwiftUI Label or the properties on UIBarButtonItem, you're most of the way there.

// SwiftUI
Label("Share", image: "arrowUp")
// UIKit
UIBarButtonItem(title: "Share", image: arrowUp)

Override the default with axis behavior

Most of the time, you shouldn't need to care where your button is placed. The system should handle that for you.

But in some special cases, we might need to give a hint to the system about our preferred placement with the new axisBehavior API.

There are two cases where this override might be needed.

Some buttons swap between a symbol and text. The Edit button in Clock turns into "Done". A symbol could go vertical, but text can't, and an item that jumps between bars as you tap it feels broken.

World Clock on iPhone Duo: Edit as text at the top, then Done as an orange checkmark in the same spot. Neither moves to the vertical tab bar.
Edit and Done stay on the horizontal axis so the control doesn't jump bars. Image: Apple, Raise the bar with iPhone Duo, 8:16 and 8:21.

The system Edit button already handles this. For a custom button that swaps between a symbol and text, mark it horizontal only:

SwiftUI and UIKit code setting axisBehavior to horizontalOnly on a SelectOrDoneButton, beside Trip Planner on iPhone Duo with Done remaining in the top corner.
A custom select button stays horizontal with .axisBehavior(.horizontalOnly). Image: Apple, Raise the bar with iPhone Duo, 8:40.
// SwiftUI
.toolbar {
ToolbarItem {
SelectOrDoneButton()
}
.axisBehavior(.horizontalOnly)
}
// UIKit
item.axisBehavior = .horizontalOnly

Let a custom view go vertical

The system keeps custom views and complex views horizontal by default, because it can't tell whether they'll fit a fixed width.

If yours does work vertically, say so with .axisBehavior(.verticalPreferred):

// SwiftUI
.toolbar {
ToolbarItem {
CompassView()
}
.axisBehavior(.verticalPreferred)
}
// UIKit
let item = UIBarButtonItem(customView: CompassView())
item.axisBehavior = .verticalPreferred

Making that view actually work in a vertical bar takes a bit more than the flag. That's the next article.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Summary

Vertical bars have a fixed width, so they favor symbol-only items. Keep giving the system both an icon and a title and let it choose. Use .horizontalOnly for items that swap between a symbol and text, and .verticalPreferred for custom views that can adapt.

In the next article, we get those custom views ready for the vertical axis.


Read more article about iOS, SwiftUI, UIKit, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Opt in to vertical bars on iPhone Duo

What it takes to get a vertical bar, which containers get one, and the order items appear in.

← Home