iOS: UIButton resize according to text length

后端 未结 14 823
你的背包
你的背包 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:09

    Simply:

    1. Create UIView as wrapper with auto layout to views around.
    2. Put UILabel inside that wrapper. Add constraints that will stick tyour label to edges of wrapper.
    3. Put UIButton inside your wrapper, then simple add the same constraints as you did for UILabel.
    4. Enjoy your autosized button along with text.

提交回复
热议问题