swiftcode / Pool

3 stars 1 forks source link

Easier constraints #11

Closed swiftcode closed 2 years ago

swiftcode commented 2 years ago

The code for setting the constraints can be replaced by an extension to UIView which I just added.

For example:

        NSLayoutConstraint.activate([
            week.topAnchor.constraint(equalTo: topAnchor, constant: 80),
            week.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 80),
            week.heightAnchor.constraint(equalToConstant: 35),
            week.widthAnchor.constraint(equalToConstant: 80),

            [...]
        ])

Can be replaced with something similar to this:

week.addConstraint(topAnchor: topAnchor, leadingAnchor: leadingAnchor, trailingAnchor: 0.0, bottomAnchor: 0.0, paddingTop: 80.0, paddingLeft: 80.0, paddingRight: 0.0, paddingBottom: 0.0, width: 35.0, height: 80.0)

JaredP45 commented 2 years ago

I am having difficulty with this issue; I assume that the addConstraint will still be inserted within NSLayoutConstraint..., but it's not working out as well. I have also tried just having the addConstraint inside of setupLayout, but again it's not building and throws errors. I also checked the documentation for addConstraint but I didn't find much.

swiftcode commented 2 years ago

No worries. This code is stand-alone and doesn't go inside a NSLayoutConstraint block. If you look in MoreViewController and MoreView, there are examples of how to use this. If nothing is working, feel free to ping me during the day or we can walk through it in study hall.

Edited to add: Make sure that translatesAutoresizingMaskIntoConstraints = false is in the code for each view you're setting the constraints for.