Text overlaps on If conditional in ForEach statement SwiftUI

Viewed 208

I'm trying to show Texts from a ForEach loop after doing an if check. Even though I have VStack inside and outside the ForEach, the text objects appear to overlap. I'm exploring other options as filtering the array but don't understand how to apply that to my conditional. Please let me know if there's an easy way to either have the results inside a VStack or filter the data.

struct LikedView: View {
    @ObservedObject var users = getUserData()
    @ObservedObject var globalCount = GlobalCount()
    @ObservedObject var movies = getMoviesData()
    @State var likedArray = []
    @State var count = 0
    
    var body: some View {
        //Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
        ZStack{
            ForEach(self.users.datas) { alpha in
                ForEach(self.movies.datas) { movies in
                    VStack{
                        ForEach(alpha.loved, id: \.self) { item in
                            VStack{
                                if(movies.id == item){
                                    Text(item)
                                }
                            }
                        }
                    }
                }
            }
        }
        .onAppear{
            self.users.fetchData()
            self.movies.fetchData()
        }
        
    }
}

enter image description here

1 Answers

What is happening in your code is you are putting the forEach loop in a ZStack which will render the views on top of the previous one. Try to wrap the forEach loop within HStack or VStack then they will be properly positioned.

Related