UIButtonBackground图像小于按钮框架。我如何平等?

时间:2012-12-07 18:59:37

标签: ios image uibutton uikit

我正在换肤UIButtons,我发现按钮的宽度比图像显示的宽度宽。

我用指定的框架和大小调用下面的按钮方法:

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;
}

请注意,添加了黄色背景以说明按钮框架的实际大小。为什么我的图像不是那么大? enter image description here

这是我的按钮图片:enter image description here

1 个答案:

答案 0 :(得分:1)

我在photoshop中查看了你的按钮图像,看起来图像周围有透明度,图像的大小比画布小。图像仅为43x44像素,周围有透明区域。

我剪掉了透明区域。试试这个图片。

enter image description here