Search code examples
iosobjective-cuibuttonuiedgeinsets

Center UIButton image with inset


I'd like to create a UIButton, but with a larger tap area than the image. (Ex: 40x40 button, but the image is only 20x20, centered).

Is that what imageEdgeInsets is for?

I've set it both programatically: (This is in the UIView which contains my button)

- (void)awakeFromNib {
    [_plusButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [_plusButton setContentMode:UIViewContentModeCenter];
}

And from storyboard

enter image description here

But neither of them seem to work.


Solution

  • Got it I think that you set your image as a background image of the button, set it as a image then it will never stretches(remains in it original shape) and imageEdgeInsets will works on it properly.

    Like:

    enter image description here