GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: How to get rid of NavigationLink space

Forums > SwiftUI

@kassi  

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?

2      

@kassi  

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.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

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.