WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Project 6 challenges troubles

Forums > 100 Days of Swift

Hi to all, I'm trying to solve the challenges in project 6

https://www.hackingwithswift.com/read/6/6/wrap-up

The first was quite easy (Try replacing the widthAnchor of our labels with leadingAnchor and trailingAnchor constraints, which more explicitly pin the label to the edges of its parent.)

I'm not sure to understand what "try using the safeAreaLayoutGuide for those constraints" means

The third challeng is really hard: Try making the height of your labels equal to 1/5th of the main view, minus 10 for the spacing.

After half an hour of obscure coding I've read the hint, so I tried

label.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor, multiplier: 0.5, constant: 10)

but the results is not what I expected to be...

Any hint?

2      

Looks like nobody solved these challenges... 🤔

1      

1/5th is 0.2 not 0.5

Make that change and see if you get what you expect.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

I think he wants you to use the safeAreaLayoutGuide to the the spacing on the sides in landscape mode. As for the one fifth, 1/5 = 0.2, as noted in the above comment.

1      

Thanks for your answers but changing that value doesn't change anything, visually talking... I'm sure that I miss something but I really can't understand what's the problem... I think I'll try to follow again the entire project videos...

1      

I just did that exercise half an hour ago and had the same problem as you. After trying all kinds of combinations, I finally thought about what the constant is actually supposed to do... It adds space to your view. That is the opposite of what we want. We want to delete 10 points from the label and use that space for the white space between the labels. So... instead of constant: 10 we have to use constant: -10.

4      

Hi All I think I solved challenge 1 and 2 as well from this project from what I understood goes like this: Whe you guys will rotoate your device or / simulator than that label will not be streched fully on the whole screen, "so the label is pushed away from the top and bottom of the safe area, so it doesn’t sit under the notch". Hope it helps!

Note: If you guys don't add "safeAreaLayoutGuide" to these constraits the labels will be still streched out fully on the screen this is in challenge 1.

    for label in [label1, label2, label3, label4, label5] {

// label.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true -> this is the previous line what I've replaced

        label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true 
        label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true   
        label.heightAnchor.constraint(equalToConstant: 88).isActive = true

1      

label.heightAnchor.constraint(equalToConstant: view.frame.height/5).isActive = true

i just did this, but i dont know how to make it autoresize when its horizontal view.

1      

The solution for all 3 challenges:

for label in [label1, label2, label3, label4, label5] {
            label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
            label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true

            let spaceBetweenLabels = 10
            label.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor,
                                          multiplier: 0.2,
                                          constant: - spaceBetweenLabels).isActive = true
            // it🔝 means:
            // each label's height should be 1/5 or 0.2 OF THE safe area's height
            // minus space between labels

            if let previousLabel = previousLabel {
                label.topAnchor.constraint(equalTo: previousLabel.bottomAnchor, constant: 10).isActive = true
            } else {
                label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
            }

            previousLabel = label
        }

   

Thanks for sharing amazing information keep posting. sjolingbogren.se Thank you for such an amazing discussion.

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out 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.