Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 983
miorel Avatar asked Mar 24 '10 14:03

miorel


People also ask

How do I change the background color in programmatically in Swift?

Method 2 − Programmatically changing the backgroundCreate outlet of the button on the View Controller. In the viewDidLoad() or viewWillLayoutSubview() method add the code to change the background color.

How do I change the background color in Objective C?

backgroundColor =[UIColor colorWithRed:178/255. f green:14/255. f blue:12/255. f alpha:0.05];

How do I change the background in Swift?

Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.


2 Answers

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.

like image 128
Warrior Avatar answered Sep 19 '22 03:09

Warrior


The color must be a UIColor object:

self.view.backgroundColor = [UIColor redColor];
like image 36
Ole Begemann Avatar answered Sep 22 '22 03:09

Ole Begemann