简体   繁体   中英

UIView - resize to fit contents

I have created a subclass of UIView that has this property

@propert (nonatomic, copy) NSArray *tags;

The tags array contains a list of strings that the view should display. The view should adjust its size according to the number of tags it displays. Who should be responsible for adjusting the view's size?

  1. Should it be the view itself? (in response to property change)
  2. Should it be the controller managing the view?
  3. Should the controller managing the view ask it for its current size? (after setting the tags property)

Superview is responsible for placement of subviews. However in certain scenarios the size of subview can vary than the static size specified by superview. In such cases superview can be relayout. Each subview is then queried for sizeThatFits: to return the precise size.
So, override sizeThatFits: method in subview and return the dynamic size that is required by the view to precisely fit its content/subviews.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM