Home Ios Intrinsiccontentsize
Post
Cancel

Ios Intrinsiccontentsize

Official Doc

1
var intrinsicContentSize: CGSize { get }

The natural size for the receiving view, considering only properties of the view itself.

Custom views typically have content that they display of which the layout system is unaware.

Setting this property allows a custom view to communicate to the layout system what size it would like to be based on its content.

This intrinsic size must be independent of the content frame, because there’s no way to dynamically communicate a changed width to the layout system based on a changed height, for example.

References

advanced-auto-layout-toolbox

The intrinsic content size is the size a view prefers to have for a specific content it displays.

For example, UILabel has a preferred height based on the font, and a preferred width based on the font and the text it displays.

A UIProgressView only has a preferred height based on its artwork, but no preferred width.

A plain UIView has neither a preferred width nor a preferred height.

You have to decide, based on the content to be displayed, if your custom view has an intrinsic content size, and if so, for which dimensions.

To implement an intrinsic content size in a custom view, you have to do two things: override intrinsicContentSize to return the appropriate size for the content, and call invalidateIntrinsicContentSize whenever something changes which affects the intrinsic content size. If the view only has an intrinsic size for one dimension, return UIViewNoIntrinsicMetric /NSViewNoIntrinsicMetric for the other one.

Note that the intrinsic content size must be independent of the view’s frame. For example, it’s not possible to return an intrinsic content size with a specific aspect ratio based on the frame’s height or width.

Q & A

iOS Container View doesn’t honour intrinsicContentSize

Auto layout uses the intrinsicContentSize property. If you’re not using auto layout, the system will not ask any view for its intrinsicContentSize.

This post is licensed under CC BY 4.0 by the author.