miorel
miorel

Reputation: 1833

How to programmatically change UIColor of view

Okay, this question comes through a friend so it might be lost in translation...

Basically we need to change the color on a View. It appears to be stored in NSString format, but using a NSString to set the UIColor doesn't seem to do anything. In other words, if NSString color holds the value "redColor" then:

self.view.backgroundColor = color;  //does nothing

Disclaimer: we are Objective-C/iPhone newbies.

Upvotes: 8

Views: 19583

Answers (5)

puneeth
puneeth

Reputation: 753

The simplest way to add color programmatically is by using ColorLiteral.

Just add the property ColorLiteral as shown in the example, Xcode will prompt you with a whole list of colors which you can choose. The advantage of doing so is lesser code, add HEX values or RGB. You will also get the recently used colors from the storyboard.

Example: self.view.backgroundColor = ColorLiteral enter image description here

Upvotes: 0

Warrior
Warrior

Reputation: 39374

Try

self.view.backgroundColor = [UIColor redColor];

You can also give RGB values like

self.view.backgroundColor = [UIColor colorWithRed:200/255.0 green:0/255.0 blue:67/255.0 alpha:1.0];

All the Best.

Upvotes: 23

iOS Developer
iOS Developer

Reputation: 138

like this also we can use

[self.view setBackgroundColor:[UIColor redColor]];

Upvotes: 1

faisal goraya
faisal goraya

Reputation: 21

self.view.backgroundColor = [UIColor blueColor];

OR do like this

self.view.backgroundColor = [UIColor colorWithRed:180.0/256.0 green:180.0/256.0 blue:180.0/256.0 alpha:1.0];

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135548

The color must be a UIColor object:

self.view.backgroundColor = [UIColor redColor];

Upvotes: 1

Related Questions