0

How can I make a UIButton with a transparent background but a non-transparent title? I tried the following

button.alpha = .5;
button.titleLabel.alpha = 1;

That did not have the desired effect. Other suggestions?

helloB
  • 3,472
  • 10
  • 40
  • 87

3 Answers3

1

just make only the background 0.5 alpha

button.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:0.5];
Mohamed Helmy
  • 821
  • 1
  • 9
  • 20
1

Just try playing with different parts of your button.


For example, if that is the image, that you want with alpha 0.5, then try this:

[button setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];

button.imageView.alpha = 0.5;

or even this:

button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage"]];

button.backgroundColor = [button.backgroundColor colorWithAlphaComponent:0.5];

You can even add alpha directly onto UIImage -- How to set the opacity/alpha of a UIImage?

The above link would help if you are setting the image via:

[button setBackgroundImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];

I don't know other solutions, though. Hope this helps.

Community
  • 1
  • 1
OlDor
  • 1,460
  • 12
  • 18
0

you can use this: swift button.backgroundColor = UIColor.white.withAlphaComponent(0.7)

Marwan Alqadi
  • 795
  • 8
  • 14