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

Visual Glitch when using *Stack view inside of CarouselStack item view #74

Closed
1er4y opened this issue Jul 23, 2023 · 3 comments · Fixed by #75
Closed

Visual Glitch when using *Stack view inside of CarouselStack item view #74

1er4y opened this issue Jul 23, 2023 · 3 comments · Fixed by #75
Assignees
Labels
bug Something isn't working

Comments

@1er4y
Copy link

1er4y commented Jul 23, 2023

Describe the bug
Hi. Thanks for library - very helpful.
Got some issue here and can't solve few days
I'm trying to create CarouselStack Item view with complex layout but getting visual glitches inside of it

I need to split my layout into few subviews because its complex and contains many Stacks for alignment and content, but can't get it working.

I've created sample project for minimal reproducing, see Additional Context

To Reproduce
Minimum amount of code to reproduce:

ContentView.swift

import SwiftUI
import CarouselStack

struct ContentView: View {
    var ints = [1,2,3,4,5,6,7,8,9]
    var body: some View {
        CarouselStack(ints, initialIndex: 0) { int in
            CustomView(passedInt: int)
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CustomView.swift

import SwiftUI

struct CustomView: View {
    let passedInt: Int
    var body: some View {
        VStack { //works if replace VStack with Non-Stack view
            if passedInt % 2 == 0 {
                viewOne.padding()
            }
            else {
                viewTwo.padding()
            }

            subscription
                .padding()
        }
        .background(.gray)
    }

    var viewOne: some View {
        Group {
            Text("I like this Int: \(passedInt)")
                .foregroundColor(.yellow)
                .font(.title3)
                .background(content: {
                    ZStack {
                        Color(.blue)
                    }
                })
        }

    }

    var viewTwo: some View {
        Group {
            Text("I dont like this Int: \(passedInt)")
                .foregroundColor(.white)
                .font(.title)
                .background(content: {
                    ZStack {
                        Color(.blue)
                    }
                })

            Text("\(passedInt)")
                .foregroundColor(.white)
                .font(.title)
                .background(content: {
                    ZStack {
                        Color(.green)
                    }
                })
        }
    }

    var subscription: some View {
        VStack {
            Text("Please tell your favorite Int")
                .foregroundColor(.red)
        }

    }
}

struct CustomView_Previews: PreviewProvider {
    static var previews: some View {
        CustomView(passedInt: 1)
    }
}

Steps to reproduce the behavior:

  1. Download attached project and run

Expected behavior
Everything works correctly

Screenshots
Screenshot 2023-07-24 at 02 18 00

Device information (please complete the following information):

  • Device: Simulator
  • OS: 16.2
  • Xcode version: 14.2

Additional context
MinimalProject.zip

@1er4y 1er4y added the bug Something isn't working label Jul 23, 2023
@dscyrescotti
Copy link
Owner

@1er4y Thank you for raising this issue with minimum project. 😃

I had a quick look and figured out that SizePreferenceKey is wrongly triggered with CGSize.zero resulting the breakdown of CarouselStack's appearance. So it could be temporarily fixed by skipping CGSize.zero value inside onPreferenceChange(_:_) modifier as follows.

.onPreferenceChange(SizePreferenceKey.self) { size in
    if size != .zero {
        DispatchQueue.main.async {
            self.size = size
        }
    }
}

@1er4y
Copy link
Author

1er4y commented Jul 24, 2023

I had a quick look and figured out that SizePreferenceKey is wrongly triggered with CGSize.zero resulting the breakdown of CarouselStack's appearance. So it could be temporarily fixed by skipping CGSize.zero value inside onPreferenceChange(_:_) modifier as follows.

.onPreferenceChange(SizePreferenceKey.self) { size in
    if size != .zero {
        DispatchQueue.main.async {
            self.size = size
        }
    }
}

Thank you for workaround - going to apply this as a temporary fix and will wait for fix versions!

@1er4y Thank you for raising this issue with minimum project. 😃

Everything for nice library =)

@dscyrescotti
Copy link
Owner

The issue is now closed since it has been resolved and the fix is available in Version 2.1.3.

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

Successfully merging a pull request may close this issue.

2 participants