Having trouble getting UIView sizeToFit to do anything meaningful

后端 未结 4 645
陌清茗
陌清茗 2020-12-09 02:07

When I add a subview to a UIView, or when I resize an existing subview, I would expect [view sizeToFit] and [view sizeThatFits] to ref

4条回答
  •  悲哀的现实
    2020-12-09 02:48

    The documentation is pretty clear on this. -sizeToFit pretty much calls -sizeThatFits: (probably with the view's current size as the argument), and the default implementation of -sizeThatFits: does almost nothing (it just returns its argument).

    Some UIView subclasses override -sizeThatFits: to do something more useful (e.g. UILabel). If you want any other functionality (such as resizing a view to fit its subviews), you should subclass UIView and override -sizeThatFits:.

提交回复
热议问题