5

I created a windowbased project,then I added a new viewcontroller with xib for user interface checked,and then by entering into the xib,I made some customization to the view,I added few buttons and also changed the background color,

But now I want to change the background color of the view through code and not through xib,

so I tried this in my

-(void)viewDidLoad
{
    UIColor *myColor = [UIColor colorWithRed:(128.0 / 255.0) green:(90.0 / 255.0) blue:(200.0 / 255.0) alpha: 1];
    self.view.backgroundcolor = mycolor;

}

but nothing change happened,so please help me out friends

Thanks & Regards Ranjit

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • have you tried something like [UIColor redColor] and had it work? – akaru Nov 04 '11 at 09:45
  • I tried this code and it didn't work because the color was assigned to myColor and the backgroundcolor was set to mycolor. Case difference. I know this is almost 17 months old, so hopefully you resolved it. Once I changed this, I got a nice purple color – cardmagik Mar 09 '13 at 21:50

4 Answers4

14

What you have typed is correct. It should work. But might I add some correction - define macros, it makes it easy to specify colors -

#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

[self.view setBackgroundColor: RGB(135, 182, 44)]; //will give a UIColor objct
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • hey srikar,,thanks for your valuable information,but my code is not working,,,it shows me the backgroung color which I have set through xib – Ranjit Nov 04 '11 at 08:19
1

I have run into a similar problem, and I think I know the reason. You are setting the backgroundColor in the viewDidLoad. I have found any changes I make to the background colour get erased by the time the code gets to the viewDidAppear:.

If you move your setting to the viewDidAppear:, it should work.

Alternatively, you can make it so that it sets the colour in the main thread.

[[NSOperationQueue mainQueue] addOperationWithBlock:
 ^{
     [UIColor colorWithRed:(128.0 / 255.0) green:(90.0 / 255.0) blue:(200.0 / 255.0) alpha: 1];
 }];
Erik Allen
  • 1,841
  • 2
  • 20
  • 36
1

This is how I solved my problem: You have to set the view of the XIB file to default, if you don't change it to default you will not be able to change the background colour from the original colour that was set.

For example if you set the background colour to red, even if you change it programmatically it will stay read and not only that but things like view.layer.cornerRadius will not work as well.

So basically change the background colour of the view in the XIB file to default and set it programmatically.

YellowPillow
  • 4,100
  • 6
  • 31
  • 57
0

There must be something else wrong with your code, this is the way to do it. Try printing out self.view with NSLog, is it a valid object? Are you sure that code is getting called? Is that view really in the view hierarchy?

D.C.
  • 15,340
  • 19
  • 71
  • 102
  • hey darren,when i set the color through xib,it changes ,but when i try to do it through code..its not working..and its a valid project..I tried printing the values in xib..and I get the correct rgb values,but the color wont change – Ranjit Nov 04 '11 at 08:20