jamone
jamone

Reputation: 17421

UIButtonBackground image smaller then button frame. How do I make equal?

I am skinning UIButtons and I've found that my button's width is much wider then what is displayed by the images.

I call the button method below with a specified frame and size:

CGSize buttonSize = CGSizeMake(240, 64);

+ (UIButton *)buttonWithThemeAndFrame:(CGRect )frame title:(NSString *)title target:(id)target selector:(SEL)selector {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    button.backgroundColor = [UIColor yellowColor];
    NSString *defaultName = @"button_main_up";
    NSString *highlighted = @"button_main_down";
    [button setBackgroundImage:[[UIImage imageNamed:defaultName] stretchableImageWithLeftCapWidth:12 topCapHeight:12.0] forState:UIControlStateNormal];
    [button setBackgroundImage:[[UIImage imageNamed:highlighted] stretchableImageWithLeftCapWidth:12 topCapHeight:12.0] forState:UIControlStateHighlighted];

    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.5] forState:UIControlStateDisabled];
    button.titleLabel.shadowOffset = CGSizeMake(0.0, -1.0);
    button.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    button.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;

    return button;
}

Note the yellow background is added to illustrate what size the button's frame actually is. Why isn't my image that size also? enter image description here

Here is my button image:enter image description here

Upvotes: 0

Views: 528

Answers (1)

Srikanth
Srikanth

Reputation: 1735

I looked at your button image in photoshop and it looks like there is a transparency around the image and the size of the image is smaller than the canvas. The image is only 43x44 px with a transparent area around it.

I have clipped the transparent area. Try with this image.

enter image description here

Upvotes: 1

Related Questions