UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Bug while getting SubViews of a ZStack to be the same size

Forums > SwiftUI

EDIT: Apologies for the incredibly large images 😅

Hi there!

I am trying to get two HStacks to be the same size where one of them dictates the size. This works, initially, but there's a bug that I just can't seem to find a solution to. It's easier to demonstrate this using the design of what I'm trying to achieve:

Design of the app

Then when you swipe, depending on left or right, this is what will show up:

Swipe right

Swipe left

In order to get that to work I have a VStack with a List view that renders out this card view:

@State private var cardDimension = CGSize.zero

var body: some View {
    ZStack {

        // This HStack is for the swipe action markers
        HStack {...}
          .frame(width: cardDimension.width, height: cardDimension.height)

        // This HStack has the content and dictates the size
        HStack {...}
          .background(
              GeometryReader { geo in
                  Text("")
                      .onAppear(perform: {
                          self.cardDimension = geo.size
                  })
              }
          )
    }
}

This works and has the desired result, as you could see in the first image. However, here's where things get interesting. When I add a new view to the parent view of the VStack that has the card views, this is what happens:

Weird bug

NavigationView {
    ZStack {
        Color("backgroundPrimary")
            .ignoresSafeArea(.all)

        // This VStack has the List view with all the Card views
        VStack {...}

        // Adding this VStack causes the geometryreader for the background of the first card in the previous VStack to go crazy.
        VStack {...}
    }
    .navigationTitle(Text("My wishlist"))
}

Without the second VStack, which contains the button, everything works as expected. When adding the button, suddenly the HStack no longer calculates it's width and height properly.

The strangest thing about this all is that it's only the first Card view where this happens. Everything after the first renders out as expected.

I've tried using PreferenceKeys instead, no luck there. I'm a bit lost now and I can't seem to figure this out. Any help would be much appreciated!

3      

After weeks of trial and error I ended up finding a solution myself! The onAppear() runs, obviously, as soon as the view appears. This is, however, too fast. Adding a 0.01s delay using DispatchQueue like so:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {...}

There's probably better solutions out there, but this definitely works!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.