Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center UIButton image with inset

Tags:

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.

like image 906
Lord Zsolt Avatar asked Jun 02 '14 09:06

Lord Zsolt


People also ask

How do you center an image in UIButton?

You must set the content mode for the image inside the UIButton. The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton .

What is UIEdgeInsets in Swift?

Returns a string formatted to contain the data from an edge insets structure. class func uiEdgeInsets(for: String) -> UIEdgeInsets. Returns a UIKit edge insets structure based on the data in the specified string.

How do I use imageEdgeInsets?

The rule when it comes to titleEdgeInsets or imageEdgeInsets is to add equal and opposite offsets to the left and right insets. So if you add for example 8 points to the left title inset, you need to apply -8 points to the right inset.


1 Answers

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

like image 89
Irfan Avatar answered Sep 21 '22 23:09

Irfan