NEW: Learn to build the incredible iOS 15 Weather app today! >>

Token list/field view... or something like it?

Forums > SwiftUI

I know SwiftUI doesn't come with a TokenList built in (maybe some day?) So I'm trying to simulate one.

The situation: I've got a book with multiple authors (say, like an anthology.)

I've created a "quasi-token" view and put it in a horizontal list:

    private func personToken(_ person: NamedPerson) -> some View {

    private var authorTokenList: some View {
        HStack {
            ForEach(book.authors) { author in

Which looks pretty good at first glance:


My first problem (reserving the possibility to expand as further issues become apparent) is that the tokens themselves wrap if the HStack gets too wide for the view:


It would be better if the HStack resized vertically to accomodate more tokens, but while I can find tutorials on making a scrollable horizontal view, I'm not sure how to make a vertically-resizable HStack.

I'm also going to want to add context-menu like functionality to each token on a long press gesture (maybe?) to delete or re-order the tokens, but I need to try to work that out before I have any specific questions about it.


To keep the tokens from wrapping you can apply a .lineLimit(1) modifier to the Text, but I'm not sure off the top of my head what you could use rather than an HStack for a vertically expanding container.


For vertically resizing to accomodate more tokens, take a look at this: Flexible layouts in SwiftUI | FIVE STARS Might be useful.


You could also look at if LazyVGrid, LazyHGrid, LazyHStack or LazyVStack (in combination any other ideas here) works for you.


Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.