Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vnraju

macrumors newbie
Original poster
Jun 27, 2007
25
0
hi all,
is it possible to change the background color of a window?
if it is possible how it is?
any suggestion is appreciated..
 
You can change the background of a view by making a custom subclass of NSView and implementing this method:

Code:
- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	NSColor *white=[NSColor whiteColor];
	[white set];
	NSRectFill([self bounds]);
}

This code just makes the view white, but another colour is easy to achieve by changing "whiteColor" to the color of your choosing.
 
You never said whether you meant your OWN application, or ANY application. And if it's your own, what are you using to code it?
 
yaaa its my own app...
im trying to change using the code
[myWindow setbackgroungColor:[NSColor blackColor]];
but it....doesn't work....[/COLOR]
 
1. Do you get an error, or just nothing happens?

2. Can you post the actual code you're using to do it? In the example you give, the call is misspelled (but probably just a typo here?)

3. Are you sure that myWindow is a valid, initialized window? For example, is it connected to a window in IB? Are you trying to call setBackgroundColor: in a method that gets called before awakeFromNib:? If you are, the window might be invalid or not receive messages yet.
 
1. Do you get an error, or just nothing happens?

2. Can you post the actual code you're using to do it? In the example you give, the call is misspelled (but probably just a typo here?)

3. Are you sure that myWindow is a valid, initialized window? For example, is it connected to a window in IB? Are you trying to call setBackgroundColor: in a method that gets called before awakeFromNib:? If you are, the window might be invalid or not receive messages yet.

no it doesn't raise any error..
i made a mistake in posting...to this forum...

and that method will be called after te awakefromnib method.....
 
It looks like the problem is related to HiRez's #3 item. You could try NSLog-ing your myWindow variable on awakeFromNib, and see if it shows up as a valid object. If not, then there's something wrong with your nib.
 
yaaa its my own app...
im trying to change using the code
[myWindow setbackgroungColor:[NSColor blackColor]];
but it....doesn't work....[/COLOR]

Change your window style to textured. Does that work now?
Also, make sure to tell the [window display] after setting the color.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.