Eduardo Iglesias
Eduardo Iglesias

Reputation: 1049

Layout UIView without adding it to subview

I'm creating a Grid view that reuses Cells, supports header and footer and also supports constraints.

But I'm getting a bad time with the layout of the constraints, because I add everything to a ScrollView, the constraints doesn't want to work. I already try

sizeThatFits setNeedsUpdateConstraints updateConstraintsIfNeeded setNeedsLayout layoutIfNeeded

I'm doing this because I didn't find any grid View that supports everything that was developed in swift or obj-c

Any idea on how I can force that the UI calculate the Constraints before adding it to the Scroll view?

Upvotes: 1

Views: 368

Answers (1)

Dave Batton
Dave Batton

Reputation: 8835

Any idea on how I can force that the UI calculate the Constraints before adding it to the Scroll view?

You can try calling -layoutIfNeeded, but it may not work. The OS is probably optimized not to layout views that are not part of the view hierarchy. Even if it works, it will get laid out again when it's added as a subview to another view.

But I'm getting a bad time with the layout of the constraints, because I add everything to a ScrollView, the constraints doesn't want to work.

To get auto layout working with a scroll view, you'll want to add all of your objects to a single view, then add that one view to the scroll view.

Using scroll views with auto layout is tricky. I still cringe every time I have to do it.

Upvotes: 3

Related Questions