Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of intrinsic size vs system width/height constraints?

Starting to move from springs and structs layout to auto layout and have some questions regarding "Intrinsic Size" setting.

If you set "Intrinsic Size" of some view to "Placeholder" and put some width and height values then the view will not scale and will stay of the fixed size. But if we set "Intrinsic Size" to "Default (System defined)" then the system will decide and scale the view if needed during runtime.

However, we could explicitly set width and height system constraints to the view by using Ctrl + Drag. In such case there would be system width and height constraints AND const placeholder values. I'm reviewing existing code and trying to understand is it a bug and redundant system width and height constraints should be removed OR there's some other hidden logic. However, Xcode does not show any warnings and etc in console during runtime. Here's a print screen of demo project:

enter image description here

like image 879
Centurion Avatar asked Oct 30 '13 08:10

Centurion


People also ask

What is intrinsic content size?

Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label's intrinsic content size is based on how much text it is displaying. In your case, the image view's intrinsic content size is the size of the image that you selected.

How do you calculate intrinsic size?

For images the intrinsic size has the same meaning — it is the size that an image would be displayed if no CSS was applied to change the rendering. By default images are assumed to have a "1x" pixel density (1 device pixel = 1 CSS pixel) and so the intrinsic size is simply the pixel height and width.

How does a view's intrinsic content size aid in auto layout?

In general, the intrinsic content size simplifies the layout, reducing the number of constraints you need. However, using the intrinsic content size often requires setting the view's content-hugging and compression-resistance (CHCR) priorities, which can add additional complications.


1 Answers

When you set the intrinsic size to "Placeholder", you tell the Xcode layout system that your view will have size dependent on its content (like a label can be dependent on its text content). The system only knows and maintains its own types of views with intrinsic size. If you wish to have a similar experience with a view of your own, to consider the view as if it has an intrinsic size. Then you set up your constraints as if the view should grown or shrink depending on its content. Finally, you implement the intrinsicContentSize method to calculate and return the correct size of the content. If the content changes and a new calculation should be performed, you call invalidateIntrinsicContentSize on your view. Depending on how you have set up your constraints, your view will either grow and/or shrink, or it will be static (like you can set a width and height constrains on a label, and it remains static).

like image 127
Léo Natan Avatar answered Oct 07 '22 17:10

Léo Natan