rounak
rounak

Reputation: 9397

Interface Builder runtime autolayout constraints preventing view from occupying intrinsic size, how do I fix this?

I have a custom UIView subclass which has an intrinsicContentSize. The view is declared in IB, with 3 constraints, center-x, height and bottom margin. When the app runs, I want the view to take up width equal to intrinsic width.

However, on debugging it seems Interface Builder adds its own run time constraints for width, that override intrinsic width. How do I prevent this?

Upvotes: 2

Views: 1496

Answers (1)

Guillaume Algis
Guillaume Algis

Reputation: 11016

First, make sure you implemented intrinsicContentSize correctly, by specifying no height:

- (CGSize)intrinsicContentSize
{
    return CGSizeMake(100.0f, UIViewNoIntrinsicMetric);
}

Then, if the sole constraints on your view are center-x, height and bottom-margin, IB should prompt a ambiguous layout with a little red arrow in the top-right corner of the document outline (the list of views on the left of the canvas).

ambiguous constraints indicator ambiguous constraints details

To make IB happy, and prevent it to add the missing constraints when building the app, you must tell it that the view has a custom intrinsic content size. To do so, select the view, select the Size Inspector panel on the right, and at the very bottom change Intrinsic Size from "Default (System Defined)" to "Placeholder".

You must then indicate the same size than returned from intrinsicContentSize:

placeholder intrinsic size

This tells IB that an intrinsic content size will be defined at runtime, and that for design time it should use these values.

Note that if you specified UIViewNoIntrinsicMetric in the method implementation you should check "None" in IB. Also, the values you type here have no impact at run time. This is just an indicator for IB at design time.

Upvotes: 2

Related Questions