Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton background image offset

Is there a way to add some sort of offset to UIButton's background image?

I've got some custom background images for UIButton. These images do have a 5 pixel drop shadow area around the effective button area. When I use these images I have to move the title label around, adjust the frame and do some calculations to place the buttons correctly.

Ideally, I would like to set something like an offset (x = -5px, y = -5px) on that image (or the UIButton) so that when I assign the image as a background to the UIButton it is moved by that offset horizontally and vertically.

In html I would set a fixed size on the button div and then offset the background image by specifying background-position.

Is there something similar for iOS?

like image 464
gabelkonus Avatar asked Oct 21 '13 10:10

gabelkonus


1 Answers

Setting setTitleEdgeInsets: or setImageEdgeInsets: do not seem to have any effect on the background image of an UIButton but only on the image (and the title).

I ended up with overriding -(CGRect)backgroundRectForBounds:(CGRect)bounds where I inflate the bounds rectangle by the amount needed (5 in my case).

like image 154
gabelkonus Avatar answered Sep 29 '22 21:09

gabelkonus