Autolayout - intrinsic size of UIButton does not include title insets

前端 未结 12 1111
不知归路
不知归路 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 08:04

    You can get this to work in Interface Builder (without writing any code), by using a combination of negative and positive Title and Content Insets.

    enter image description here

    Update: Xcode 7 has a bug where you cannot enter negative values in the Right Inset field, but you can use the stepper control next to it to decrease the value. (Thanks Stuart)

    Doing this will add 8pt of spacing between the image and the title and will increase the intrinsic width of the button by the same amount. Like this:

    enter image description here

提交回复
热议问题