iamIcarus
iamIcarus

Reputation: 1438

Using autolayout constraints to define height minus a constant with storyboard

Is it possible to have a View with autolayout , such that the subview has equal height with parent minus a constant?

The Hierarchy is UIView > UIScrollVIew > UITableView. The view is also embedded in a UINavigationController

Upvotes: 1

Views: 2353

Answers (2)

Rory McKinnel
Rory McKinnel

Reputation: 8014

Yes. You simply pin the subview to the top and bottom of its super view with the constant set to the offset you wish.

As the outside view changes, the inner view will change to match and maintain the offset. You will need to set left and right constraints as well using the same mechanism.

Edit after discussion:

The UIScrollView needs to know the size of your content. You can not simply use auto layout without a few adjustments.

Assumption is your view layout is:

UIViewController -> UIScrollView -> UIView -> UITableView

For your UIView, pin the top, bottom, left and right to the UIScrollView.

To see things properly in storyboard you also seem to have to center the UIView horizontally and vertically. Once you add this you should see the content fill the scroll view bar any edge offsets you set in your pinning.

Pin your UITable to the UIView top, bottom, left and right.

You are going to have to adjust the width in your code somehow to the size you want to scroll horizontally.

Upvotes: 1

DanielS
DanielS

Reputation: 181

In Interface Builder:

  1. Clear constraints on your subview
  2. Select your subview
  3. Choose Pin in right bottom corner of IB view
  4. Select bottom pin and enter your constant (I entered 30), select top pin and enter 0.
  5. Select other constraints if needed.
  6. Accept with button Add x constraints enter image description here

Upvotes: 0

Related Questions