helloB
helloB

Reputation: 3582

UIButton with low alpha background but fully opaque title?

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?

Upvotes: 0

Views: 565

Answers (3)

Marwan Alqadi
Marwan Alqadi

Reputation: 855

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

Upvotes: 0

OlDor
OlDor

Reputation: 1466

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.

Upvotes: 1

Mohamed Helmy
Mohamed Helmy

Reputation: 832

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

Upvotes: 1

Related Questions