Autolayout - intrinsic size of UIButton does not include title insets

前端 未结 12 1112
不知归路
不知归路 2020-12-07 07:11

If I have a UIButton arranged using autolayout, its size adjusts nicely to fit its content.

If I set an image as button.image, the instrinsic size again

12条回答
  •  生来不讨喜
    2020-12-07 07:53

    All above did not work for iOS 9+, what i did is:

    • Add a width constraint (for a minimum width when the button doesn't have any text. The button will auto scale if text is provided)
    • set the relation to Greater Than or Equal

    Now to add a border around the button just use the method:

    button.contentEdgeInsets = UIEdgeInsetsMake(0,20,0,20);
    

提交回复
热议问题