mamaral / Neon

A powerful Swift programmatic UI layout framework.
MIT License
4.58k stars 389 forks source link

wrong height while overriding updateConstraints using AutoHeight #58

Closed ardulat closed 7 years ago

ardulat commented 7 years ago

I am overriding updateConstraints in the init, and first I have a wrong height (too much) for the labels I have aligned, and only after three times (idk why it calls it three times) accessing a function updateConstraints it gives right height. It is better to understand showing the code.

override func updateConstraints() {
        locationLabel.anchorToEdge(.top, padding: 0, width: self.frame.width, height: AutoHeight)
        seasonLabel.align(.underCentered, relativeTo: locationLabel, padding: 0, width: self.frame.width, height: AutoHeight)
        seasonLabel.alignAndFillWidth(align: .underCentered, relativeTo: locationLabel, padding: 0, height: AutoHeight)
        quantityLabel.alignAndFillWidth(align: .underCentered, relativeTo: seasonLabel, padding: 0, height: AutoHeight)
        durationLabel.alignAndFillWidth(align: .underCentered, relativeTo: quantityLabel, padding: 0, height: AutoHeight)
        requirementsLabel.align(.underMatchingLeft, relativeTo: durationLabel, padding: 0, width: self.frame.width*0.9, height: self.labelHeight)
        height = locationLabel.frame.height+seasonLabel.frame.height+quantityLabel.frame.height+durationLabel.frame.height+self.labelHeight
        print (height)
    }

In the console I have:

1306.0
1348.0
479.0

And here first two heights are too big (wrong), and the last one is right. How can I get only the last one?

mamaral commented 7 years ago

init is the wrong time to do it, as the view controller has not had the chance to properly load and set its frame. I would recommend adding it to viewWillLayoutSubviews if this is in a view controller, or layoutSubviews in a view.