BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: How to get rid of NavigationLink space

Forums > SwiftUI

Folowing code

struct EditView: View {
    var body: some View {
          NavigationView {
                Form {
                      Section("Colors") {
                          ColorList(colors: viewModel.game.gameColors)
                      }
                }
          }
    }
}

struct ColorList: View {
    let colors: [String]

    private let gridItemLayout = [GridItem(.adaptive(minimum: 44))]

    var body: some View {
        ScrollView {
            LazyVGrid(columns: gridItemLayout) {
                ForEach(colors, id: \.self) { colorName in
                    Meeple(colorName: colorName)
                }
                .padding(.vertical, 2)
            }
        }
    }
}

// Meeple is just an image
struct Meeple: View {
    // ...
    var body: some View {
        Image("meeple.2.fill")
            .resizable()
            .padding(5)
            .foregroundColor(color.color)
            .background(color.backgroundColor)
            .frame(width: 44, height: 44, alignment: .center)
            .clipShape(RoundedRectangle(cornerRadius: 5))
            .shadow(color: .primary, radius: 5)
    }
}

works and gives me this result, which works well (actually for the detail view). screenshot-1

As soon as I add a NavigationLink around the ColorList like so

                Section("Colors") {
                    NavigationLink(destination:
                                    MultiColorPickerView(
                                        selection: $viewModel.game.colors.withDefaultValue([])
                                    )
                    ) {
                        ColorList(colors: viewModel.game.gameColors)
                    }
                }

The result looks weird: screenshot-2

There's plenty of space left. Why does it break after 3 items? And how can I make it to show more in one line?

   

Ok, this could be solved on SO with adding .frame(maxWidth: .infinity). Still would like to understand, why it's needed. My guess is, that there's an implicit "disclosure indicator" next to the content and both are treated equal. However a simple layoutPriority(1) on ColorList doen't change anything, so this hypothesis kinda fails.

   

Please visit: https://github.com/twostraws/HackingWithSwift this will help you to understand the main reason.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

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.