18

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

Centurion
  • 14,106
  • 31
  • 105
  • 197

4 Answers4

35

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).

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • Hmm, setting "Placeholder" with custom width and height did not scale UILabel with respect to its content ("dependent on its content"). – Centurion Oct 30 '13 at 15:09
  • If you add width and height constraints, it will not scale the label. – Léo Natan Oct 30 '13 at 15:39
  • I just created new project and added a label with a larger frame. Then did set "Placeholder" and select "None" in width and height checkboxes. Nothing happened and label was not resized to fit it's content. I didn't set any constraints through. – Centurion Oct 30 '13 at 20:36
  • If you do not add any constraints (like spacing from edges to superview, at the minimum), then Xcode adds hardcoded constraints which also include width and height. Try adding a left space constraint to the superview, as well as top space constraint to the superview (by ctrl-dragging from view to superview). You'll see Xcode tells you the label is too big for its content. You should not set placeholder intrinsic size for labels, the system is familiar with how they work and deduces correctly. – Léo Natan Oct 30 '13 at 20:43
  • Superb. Created new project with a MyCustomView and did override intrinsicContentSize in MyCustomView class. Then added "Placeholder" with width and height, and voila. intrinsicContentSize was called and resized MyCustomView. Thanks! – Centurion Oct 31 '13 at 08:05
  • @LeoNatan Not working setting intrinsic size to placeholder when I have 2 labels. http://stackoverflow.com/questions/22172572/constraints-to-specify-when-view-size-is-dependent-on-multiple-subviews – Geek Mar 05 '14 at 05:43
4

if you familiar with wrap_content from android , the intrinsicContentSize is same as wrap_content.

Mehrdad Faraji
  • 3,744
  • 3
  • 25
  • 38
1

Every view that contains content can calculate its intrinsic content size. The intrinsic content size is calculated by a method on every UIView instance. This method returns a CGSize instance

BuLB JoBs
  • 841
  • 4
  • 20
0

When Intrinsic size set to Placeholder, containerView calculates its own height according to its subView(containeeView)

containerView grows or shrinks according to what's inside of it.

-Set width and position for view, not height -set view.intrinsicSize = Placeholder -Put a label inside view -snap label to all borders of the view -do not set height for label (or set height with >= operator. Not = operator) -set label as lines=0, lineBreak=WordWrap

According to content size, label will grow or shrink in height. view will grow or shrink height according to label height.

Add080bbA
  • 1,818
  • 1
  • 18
  • 25