Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spacing always applied around conditional views, even when they're missing #17

Closed
rocca23 opened this issue Mar 25, 2022 · 1 comment
Closed
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@rocca23
Copy link

rocca23 commented Mar 25, 2022

If a View is created based on a condition and when the condition is false, the "ghost" View is took into account when creating spacing. For example:

import SwiftUI
import WrappingHStack

struct Test: View {
    let condition: Bool

    var body: some View {
        WrappingHStack {
            view("view 1")
            if condition {
                view("view 2")
            }
            view("view 3")
            view("view 4")
            view("view 5")
            view("view 6")
        }
        .border(Color.red.opacity(0.5))
        .padding(32)
    }

    private func view(_ text: String) -> some View {
        Text(text)
            .padding(8)
            .border(Color.black)
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test(condition: false)
    }
}

The result is the following (note the double spacing between view 1 and view 3):
Schermata 2022-03-25 alle 15 36 59

There's a workaround in this case, that is applying spacing as trailing padding on each view and setting WrappingHStack.Spacing to .constant(0), but it would be nice to have it working naturally, especially since this way the trailing padding adds to the view width. Could it be possible to fix this? Thanks.

@dkk dkk added bug Something isn't working help wanted Extra attention is needed labels Mar 27, 2022
@dkk
Copy link
Owner

dkk commented Apr 27, 2022

Nice find! WrappingHStack implicitly uses spacing: .constant(8) and apparently the conditional View is passed as nil. The problem is that, even though the debugger returns true when checking against nil, an if with the same conditional will fail. I have no idea why this happens, probably some SwiftUI View magic.

The workaround is to use WrappingHStack(spacing: .constant(0)) and set the padding on the elements directly.

I will look at it again when I have time (got to run to a meeting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants