Sethypie
Sethypie

Reputation: 559

iOS Autolayout With Unknown Heights

I am trying to setup my views as follows

layout

Each view contains a number of child views such as labels and buttons. For my project, I must use autolayout. Specifically, I'm using a 3rd party library called FLKAutolayout which just makes things faster. The problem is that each of these views has a dynamic height. It's fine to pin both sides of each view to each side of the parent view. However, when it comes to arranging things vertically, I am encountering ambiguous layouts. I've tried to pin view 1 to the top of the superview, view 2's top pinned to view 1's bottom, view 3's top pinned to view 2's bottom, and so on. If I don't pin the bottom most view to the bottom of the superview, nothing shows up. If I do pin the bottom most view to the bottom of the superview, only the top most view gets stretched to the full size of the screen. If I manually constrain the heights to arbitrary amounts, things show up fine. The problem with that of course is that I don't actually know what the heights should be since they vary depending on the contents.

Is there a good strategy to manage an array of views with variable heights? Also, I think I need to add that it is possible (and likely) that the total height of the views will be greater than the size of the height of the view controller's view which contains these vertical views. Eventually I want to be able to scroll through them with a UIScrollView but for now it is okay for them to just continue on offscreen.

Upvotes: 3

Views: 1981

Answers (3)

ajonnet
ajonnet

Reputation: 215

I don't know about of FLKAutoLayout. But i had been trying a similar situation and had success with it.

As in the figure, view1,view2,view3 are linked to each other in a linear layout fashion.

Now, you should set the height constraint for each view manually. Also, make sure last view also has a vertical space constraint set with the superView.

Now, the trick is to have a manual height constraint for superView and set its priority a lower value than all other constraints. Like, if you have 1000 set as priority for other constraints, set 999 for this constraint of superView.

Now, as you render your content in view1,view2,view3, change their height accordingly through their respective height constraint(by changing its constant value). You will, find that subViews now automatically adjust in position, also the superview will adjust its height by itself.

Now, to workout things for scrollview, you just need to update the contentSize. As, changing height through constraint, does not bring out the actual height immediately, you have update the scrollview in following callback -viewDidLayoutSubViews

i hope this workouts in your scenario

Upvotes: 0

Maarten
Maarten

Reputation: 1873

Each UIView has an -(CGSize)intrinsicContentSize method. If the content of your views change dynamically, you should override the method and return the appropriate dimensions based on the contents of the view. Auto layout uses this information to calculate the height of the view. You may have to call -(void)invalidateIntrinsicContentSize to let the system know that the intrinsic size has changed. BTW: Most Apple-supplies UIKIT class already provide the proper intrinsic size.

Upvotes: 3

rdelmar
rdelmar

Reputation: 104082

I don't think there's any way to do this without setting specific heights for the views. You can make IBOutlets to each of them and modify their constant parameter in code based on the content size. I guess another way that might work is to give them arbitrary sizes in IB, then call sizeThatFits on them after any subviews are added.

Upvotes: 0

Related Questions