NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Hacking with iOS SwiftUI - Project 18 Challenges - GeometryReader

Forums > Books

Has anybody had luck with the Project 18 challenges?

I've been trying several things, but I'll limit this post to one specific issue:

Challenge 2 starts with this code from Project 5, and challenges us to offset the words lower down in the list by using GeometryReader.

VStack {
     TextField("Enter your word", text: $newWord, onCommit: addNewWord)
             .textFieldStyle(RoundedBorderTextFieldStyle())
             .autocapitalization(.none)
             .padding()

     List(usedWords, id: \.self) {
             Image(systemName: "\($0.count).circle")
             Text($0)
     }
}

I've been able to offset the Text view based on its position in the List with the code below.

My issues are:

(1) how to access the value of each string from the usedWords array when creating the Text in the List? I can no longer simply use $0 to read the value once the Text is in a GeometryReader. Is there a way to enumerate the List so I can get the appropriate value for each word in the list?

(2) what's a good way to apply the offset only to words that are later in the list? If I have an enumerated list I could apply the offset only after a certain number of words. I've hacked it together here based on the word's position within the bounds of the entire screen.

VStack {
    TextField("Enter your word", text: $newWord, onCommit: addNewWord)
        .textFieldStyle(RoundedBorderTextFieldStyle())
        .autocapitalization(.none)
        .padding()

    GeometryReader { geo in
        List(usedWords, id: \.self) {
            Image(systemName: "\($0.count).circle")
            GeometryReader { textGeo in
                Text(usedWords[0])     //this is very wrong, we don't want to repeat just the first word for each item in the List
                    .offset(x: textGeo.frame(in: .global).midY > UIScreen.main.bounds.size.height * 0.7 ? (textGeo.frame(in: .global).midY/geo.size.height) * 20 : 0)
            }
        }
    }
}

Thanks in advance for any help!

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.