2

我正在创建一个左侧图像和中心文本的按钮。它可以工作,但不知何故,红色的背景颜色并没有覆盖整个按钮。

    self.buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.buyButton.titleLabel setBackgroundColor:[UIColor buttonRed]];
    UIImage *image = [UIImage imageNamed:@"cart.png"];
    [self.buyButton setImage:image forState:UIControlStateNormal];
    self.buyButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [self.buyButton setTitle:@"Buy" forState:UIControlStateNormal];
    self.buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    self.buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

[1] http://s23.postimg.org/g8hxmihyv/Screen_Shot_2013_08_07_at_5_45_40_PM.png “快照”

如果我将整个按钮设置为红色,它看起来像这样。[1] http://s21.postimg.org/xdwrziphv/Screen_Shot_2013_08_07_at_5_50_15_PM.png “快照2”

4

1 回答 1

3

那是因为您只是将 titleLabel 的背景颜色设置为红色。#import <QuartzCore/QuartzCore.h>尝试在使用自定义按钮并更改按钮图层 ( )的角半径时设置按钮本身的背景颜色。前任:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor redColor]];
[button.layer setCornerRadius:5.0f];

此外,您可能需要将 titleLabel 的颜色更改为 clearColor。

于 2013-08-08T00:48:53.137 回答