BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

How to Create a Custom Tree Grid in SwiftUI

Forums > SwiftUI

Hi, how's everybody today? 😊 I'm trying to create a grid like this:

https://imgur.com/a/MeOQeTN

I have tried using ForEach and alternate between the views (1 circle, 2 circles) but it didn't work, I'm getting just one of them. Maybe someone would manage to do it this way though.

I also tried to use a grid but I can't figure out how to build a custom one.

I need each one of these circle to show a different colour and text taken from some data. So the goal is to populate the circles with actual data so it needs to be dynamic.

Thanks!

   

@Bnerd  

To understand, the data source is one, but you want to present it in the grid shown, .ie. one line with two circles and one line with one circle and so on? correct?

   

Thanks for replying :)

I will have an array of colours and I want to generate circles in this form and that each circle will have a different colour.

Basically each circle is a view that will get its data from an array. for example: ["blue", "green", "red", "pink", "orange", "yellow", "black", "grey", "white", "cyan", "purple", "lightBlue"]

I hope it makes sense now... Thank you

   

@Bnerd  

I have been trying with firstIndex of array, I can get the single views, but the doubles is a mess...

   

@Bnerd  

As you can see from the sample code below, I tried to filter the views (one circle or two circles) depending on the index number. Easily can be found that the single views are at 0 , 3, 6, etc and the doubles are at 1,5,7 etc and 2,4,6. As you can see you can get easily the single views, but although I made an HStack for the two circle view, they appear as separate.. maybe you can work around something...

struct ContentView: View {
    let array = ["☺️", "😟" , "😑", "🐡" , "🐼", "🐹", "πŸͺ±" , "πŸ¦‹", "🐝"]
    // Positions   0     1      2     3      4      5     6      7     8

    var body: some View {
        VStack {
            ForEach(array, id:\.self) { text in
                if array.firstIndex(where: {$0 == text }) == 0 || array.firstIndex(where: {$0 == text })!.isMultiple(of: 3) {
                    CircleView(text: text)
                }
                else {
                    HStack {
                        if let x = array.firstIndex(where: {$0 == text }) {
                            if (x % 2 == 0) {
                                CircleView(text: array[x])
                            }
                        }

                        if let y = array.firstIndex(where: {$0 == text }) {
                            if (y % 2 == 1) {
                                CircleView(text: array[y])
                            }
                        }
                    }
                }
            }
        }
    }
}
struct CircleView: View {
    var text = "A"
    var body: some View {
        ZStack {
            Circle().strokeBorder(lineWidth: 10).foregroundColor(.blue).frame(width: 100, height: 100)
            Text(text).font(.largeTitle)
        }
    }
}

   

Thank you very much! will take a look

   

Unfortunately I couldn't make it work.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.