alwer
alwer

Reputation: 1

Swiftui masonry with HStack = slow perfomance

I've tried to create masonry layout with SwiftUi by using HStack with two LazyVStack inside it. It works well as masonry but the performance not as good. Its just a simple masonry layout for two LazyVStack(with a lot of dynamic items from server)

struct Post : Identifiable{
  let id = UUID()
  let title: String
}
struct ContentView: View {
  let posts = [
    Post( title: "all"),
    Post( title: "nothing"),
    Post( title: "all"),
    Post( title: "nothing"),
    Post( title: "all"),
    Post( title: "nothing"),
    Post( title: "all"),
    Post( title: "nothing"),
    Post( title: "all"),
    Post( title: "nothing"),
    Post( title: "all"),
    Post( title: "nothing"), ]
  var body: some View {
    ScrollView {
      HStack(alignment: .top) {
        LazyVStack(spacing: 8) {
          ForEach(posts) { item in
            if(item.title=="all"){
              Text(item.title).frame(maxWidth: .infinity).background(.green)
            }
          }
        }
        
        LazyVStack(spacing: 8) {
          ForEach(posts) { item in
            if(item.title != "all"){
              
              Text(item.title).frame(maxWidth: .infinity).background(.red)
            }
          }
        }
      }
      
    }
  }
}

Upvotes: 0

Views: 56

Answers (0)

Related Questions