ios - How to adjust super view 's height base on subview's size in XIB? -


in xcode 6 ,i create xib custom view (named: viewa,got red background color) ,and viewa's xib got file size 600*600, in viewa ,i put subview labelb (got green background color )in ,and labelb's numberoflines = 0 ,so labelb'height variable , want viewa 's height changed based on labelb's height (e.g viewa.bottom = labelb.bottom + 10), , have pin labelb's top,bottom,trailing,leading viewa, still doesn't !work ,the viewa's height 600 ,no matter label's height . how can achieve goal in auto layout? thanks

enter image description here

using auto layout, here's need do:

  1. make sure aren't adding fixed width and/or height constraints of subviews (depending on dimension(s) want dynamically size). idea let intrinsic content size of each subview determine subview's height. uilabels come 4 automatic implicit constraints (with less required priority) attempt keep label's frame @ exact size required fit text inside.

  2. make sure edges of each label connected rigidly (with required priority constraints) edges of each other , superview. want make sure if imagine 1 of labels growing in size, force other labels make room , importantly force superview expand well.

  3. only add constraints superview set position, not size (at least, not dimension(s) want size dynamically). remember if set internal constraints correctly, size determined sizes of subviews, since edges connected theirs in fashion.

  4. make sure change view's translatesautoresizingmaskintoconstraints property value no before applying constraints.

that's it. don't need call sizetofit or systemlayoutsizefittingsize: work, load views , set text , should it. system layout engine calculations solve constraints. (if anything, might need call setneedslayout on superview...but shouldn't required.)

you can check original answer here


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -