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

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!

2      

@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?

2      

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

2      

@Bnerd  

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

2      

@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)
        }
    }
}

2      

Thank you very much! will take a look

2      

Unfortunately I couldn't make it work.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.