List in SwiftUI not appearing correctly.

I am building a list in SwiftUI.

In my view, I want the 'Jesse Lingard' and 'Manchester United' text to be next to the image but they are appearing underneath. How do I fix this?

  • Can you show your code?

Add a Comment

Accepted Reply

I found a solution.

I created my View in a separate file then embedded it as a List in my main view like so:

SoccerCell

     HStack(alignment: .center, spacing: 15){
      Image("jesse-lingard")
        .resizable()
        .scaledToFit()
        .frame(height: 70)
        .cornerRadius(8)
       
      VStack(alignment: .leading, spacing: 5){
        Text("Jesse Lingard")
          .font(.system(size: 20, weight: .bold))
          .lineLimit(1)
        Text("Manchester United")
          .foregroundColor(.secondary)
          .font(.system(size: 15, weight: .regular))
      }
    }

Main content view

     List(0 ..< 5) { item in
      SoccerCell()
    }

Replies

I found a solution.

I created my View in a separate file then embedded it as a List in my main view like so:

SoccerCell

     HStack(alignment: .center, spacing: 15){
      Image("jesse-lingard")
        .resizable()
        .scaledToFit()
        .frame(height: 70)
        .cornerRadius(8)
       
      VStack(alignment: .leading, spacing: 5){
        Text("Jesse Lingard")
          .font(.system(size: 20, weight: .bold))
          .lineLimit(1)
        Text("Manchester United")
          .foregroundColor(.secondary)
          .font(.system(size: 15, weight: .regular))
      }
    }

Main content view

     List(0 ..< 5) { item in
      SoccerCell()
    }