Amir
Amir

Reputation: 455

UILabel background color

I am trying to change the UILabel background color with this code

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    m_ShopName.text = m_CurrShop.m_Name;
    m_ShopAddress.layer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor; 
}

but nothing is happening.

Upvotes: 3

Views: 11312

Answers (5)

Ed Manning
Ed Manning

Reputation: 93

If you are using a storyboard, check to see if you have a color for the view background in the storyboard. The view background color was overriding the layer color for me. I changed the background color for the view in the storyboard to default and this fixed it for me.

So I had the code:

func select() -> Void {
    imageViewBackgroundView.layer.backgroundColor = UIColor.black.cgColor
    cellImageView.layer.backgroundColor = UIColor.black.cgColor
    cellLabel.layer.backgroundColor = UIColor.black.cgColor
    cellLabel.textColor = UIColor.white
}

The cellLabel was the view background that was not changing. I had at one time set a background color for the view in the Storyboard. Once I changed the view background for the cellLabel to default the layer color took effect.

View Background

Upvotes: 0

user4276168
user4276168

Reputation:

This will help you

UILable *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10,20,50,200)];
lbl.backgroundColor = [UIColor redColor];

Upvotes: 2

Codo
Codo

Reputation: 78855

An alpha value of 0 means, it's fully transparent. That's probably why nothings happens (whatever you mean by that).

And I wouldn't access the background color of the layer, but of the UILabel directly.

Upvotes: 0

mj_
mj_

Reputation: 6447

I always did the following

m_ShopAddress.backgroundColor = [UIColor red];

When I wanted to change the alpha...

m_ShopAddress.alpha = 0.0f;

Upvotes: 0

Rob Fonseca-Ensor
Rob Fonseca-Ensor

Reputation: 15621

Can you do this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    m_ShopName.text = m_CurrShop.m_Name;
    m_ShopAddress.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]; 
}

Upvotes: 1

Related Questions