paololeonardi / WaterfallGrid

A waterfall grid layout view for SwiftUI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Images not loading in two columns

cbartellds opened this issue · comments

I am having an issue where when I first come to the screen the images don't go into two columns like they should. After I navigate into one of the image items and return to the initial screen the images go into two columns.

Initial view of screen:
IMG_0924

Navigate away from screen:
IMG_0925

Navigate back to screen, images waterfall as they should:
IMG_0926

My code:

`
struct FavoritesView: View {

@EnvironmentObject var viewRouter: ViewRouter
@ObservedObject var favoriteItems = FavoriteItemModel()

var body: some View {
    
    HStack {
        WaterfallGrid(favoriteItems.favorites, id: \.self) { item in
            FavoriteItemView(favoriteitem: item)

        } // Waterfall
        .gridStyle(
            columnsInPortrait: 2,
            columnsInLandscape: 3,
            spacing: 8,
            padding: EdgeInsets(top: 16, leading: 8, bottom: 16, trailing: 8),
            animation: .easeInOut(duration: 0.5)
        )
        .navigationBarTitle("Favorites", displayMode: .inline)
        .onAppear(perform: getFavorites)
    } // HStack
    
}

func getFavorites() {
    favoriteItems.loadFavorites()
}

}
`

`
struct FavoriteItemView: View {

let favoriteitem: FavoriteItem
@EnvironmentObject var shareableItemVM: ShareableItemViewModel
@State private var showShareableModal: Bool = false

var body: some View {
    HStack{
        NavigationLink(destination: ShareableDetailView(shareableId: self.favoriteitem.id, categoryId: 0, categoryType: "", shareItemType: favoriteitem.itemType, showShareableModal: self.$showShareableModal)) {
                WebImage(url: URL(string: self.favoriteitem.itemImage))
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .cornerRadius(10)
        }
        .environmentObject(self.shareableItemVM)
        .buttonStyle(PlainButtonStyle())
    }
}

}
`

I am using similar code with the WaterfallGrid in a couple other views, and I do not have this issue. The only thing I can think of is because these images are at different heights.

Any thoughts?

My issue had to do with a init() in the Model.

@cbartellds glad you found the issue. Sorry I couldn't answer before.
Thanks!