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
using auto layout, here's need do:
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.
uilabel
s come 4 automatic implicit constraints (with less required priority) attempt keep label's frame @ exact size required fit text inside.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.
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.
make sure change view's
translatesautoresizingmaskintoconstraints
property valueno
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
Post a Comment