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

Dynamic VStack & HStack for a soccer app

Forums > SwiftUI

@samoi  

Hello dears!

I am starting to build my first app in swiftui, thanks for the HWS!

So, I am building a soccer app where it will display the current live matches. each match has a starting 11 players in a formation (lineup) -> see below examples of the view.

https://ibb.co/CBPhzBT https://ibb.co/r66s0j9 https://ibb.co/hRjzTsF https://ibb.co/nCnykPR

Each team has a different lineup/formation strategy. maybe 4-3-3, or 4-4-1-1 .. or any number of combinations of 11 players as you can see above in the images.

It will be so difficult to build a struct for each possible formation/lineup strategy.

is it possible to do that dynamically in VStacks/HStacks? if so, can you show an initial example of the way it's done as a guideline for me to follow?

with many thanks!

1      

hi,

given the right data structures ... perhaps using

typealias PlayerRow = [Player] // a list of players in one row of the formation

var playerRows: [PlayerRow] // a list of the rows, e.g. forwards, midfielders, defenders (as many rows as required)

... SwiftUI could show the team formation with:

VStack {
  ForEach(playerRows) { playerRow in 
    HStack {
      ForEach(playerRow) { player in
        PlayerView(player)
      }
    }
  }
}

hope that helps,

DMG

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.