iOS: UIButton resize according to text length

后端 未结 14 903
你的背包
你的背包 2020-11-29 17:28

In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the

14条回答
  •  臣服心动
    2020-11-29 18:00

    For some reason, func sizeToFit() does not work for me. My set up is I am using a button inside a UITableViewCell and I am using auto layout.

    What worked for me is:

    1. get the width constraint
    2. get the intrinsicContentSize width because according the this document auto layout is not aware of the intrinsicContentSize. Set the width constraint to the intrinsicContentSize width

    Here're two titles from the Debug View Hierachry

提交回复
热议问题