如何使UIButton背景透明?

时间:2010-05-10 16:47:21

标签: iphone uibutton

我尝试使用透明的png作为背景,但没有成功。我该如何解决?太赫兹。

6 个答案:

答案 0 :(得分:38)

在“检查器”窗口(“界面”构建器)中将“类型”更改为“自定义”对我有用。

答案 1 :(得分:21)

创建透明按钮(不使用图像):

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *layer = <your button>.layer;
    layer.backgroundColor = [[UIColor clearColor] CGColor];
    layer.borderColor = [[UIColor darkGrayColor] CGColor];
    layer.cornerRadius = 8.0f;
    layer.borderWidth = 1.0f;
}

答案 2 :(得分:7)

这也很好:

[button.layer setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.5].CGColor];

答案 3 :(得分:3)

首先,在项目中添加QuartzCore框架

其次,将其添加到标题

#import <QuartzCore/QuartzCore.h>

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(0, 0, 75, 37)];
[gradientLayer setFrame:aButton.frame];
[aButton.layer addSublayer:gradientLayer];

例如,红色渐变按钮(颜色混合)

[gradientLayer setColors:[NSArray arrayWithObjects:(id)[UIColor colorWithRed:0.841 green:0.566 blue:0.566 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.75 green:0.341 blue:0.345 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.592 green:0.0 blue:0.0 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.592 green:0.0 blue:0.0 alpha:1.0].CGColor, nil]];
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

答案 4 :(得分:1)

没有IB:

UIButton *b = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

答案 5 :(得分:0)

在IB中将按钮的类型更改为自定义按钮 - 它默认将背景设置为clearColor。

如果你必须在它的背景是透明的之前有圆角矩形UIButton,那么我想将它的背景颜色改为clearColor将无济于事。然后我建议:

  1. 从视图中删除它&amp;将其替换为具有透明背景的其他按钮,例如
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    [button setTitle:@"Title" forState:UIControlStateNormal];
  2. 不要使用苹果UIButton,子类UIControl并自己绘制它:)