Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill Proportionally in UIStackView

I am using Storyboard to create a layout which consists of a UITableView and a UIView at the bottom. I am using UIStackView and playing them vertically. I want the UITableView to take 80% of the height and UIView (footer) to take 20%. I am using Fill Proportionally option of UIStackView and even through in Xcode preview it looks good but when the app is run it does not render correctly. Here is what I want it to look like:

enter image description here

And here is what it looks like when it is run:

enter image description here

Ideas?

like image 789
john doe Avatar asked Feb 22 '18 16:02

john doe


People also ask

Is Uistackview intrinsic content size?

Managing SubviewsStack views don't have an intrinsic content size, so you must set it either implicitly with Auto Layout constraints or explicitly via its intrinsicContentSize property.

What is UISV canvas connection?

UISV-canvas-connection constraints connect subviews to the 'canvas' which is a stack view itself. The first and the last subview gets pinned to the . leading and . trailing layout attributes of the stack view respectively.


2 Answers

You will need to set an explicit constraint to make tableView.height 4x bigger than your view size. They don't have intrinsic content size, so the stackView does not "know" how to properly fill the space.

Also, set the distribution mode to .fill, because .fillProportionally uses intrinsic content size to determine proper distribution (excerpt from docs):

case fillProportionally

A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. Views are resized proportionally based on their intrinsic content size along the stack view’s axis.

Using constraints we are setting the size explicitly, not using intrinsic content size - thus .fillProportionally does not work. I assume in that case the stackView uses values returned by view.intrinsicContentSize directly. However, constraints will not change view.intrinsicContentSize.

If you would be doing it in code, you could do it with this:

tableView.heightAnchor.constraint(equalTo: myView.heightAnchor, multiplier: 4).isActive = true

In storyboards, control drag from tableView to the myView and select Equal Heights:

enter image description here

Then select the created constraint and set the appropriate multiplier:

enter image description here

like image 165
Milan Nosáľ Avatar answered Sep 16 '22 15:09

Milan Nosáľ


In the storyboard's Document Outline control-drag from the top view to the stack view and select Equal heights. Now go to the Size Inspector and you should see the constraint. Double click on it and set the multiplier to 0.2. Then update the frames.

enter image description here

Keep the stack view distribution as Fill.

enter image description here

Control drag from view2 to view1 to create equal heights constraint.

enter image description here

Go to the constraint and set multiplier as 0.2.

enter image description here

like image 38
Gihan Avatar answered Sep 19 '22 15:09

Gihan