How do I prevent a button's background image from stretching?

后端 未结 10 1932
执笔经年
执笔经年 2021-01-30 16:22

I\'m allocating a UIButtonTypeCustom UIButton to a UIView with a background image that is smaller than the button\'s frame. Reason why the image is smaller is because I\'m tryin

10条回答
  •  情话喂你
    2021-01-30 16:39

    The cleanest and easiest way it probably to use the title insets of the button.

    You set your image as the button image, and then you change the left title inset to match minus the width of your image:

    myButton.titleEdgeInsets = UIEdgeInsetsMake(0, -myImage.width, 0, 0)
    

    This will move the text back where it was before the image was added to its left. You can also use this value to add some padding to you button.

提交回复
热议问题