1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

changing the background color of a Window in an application

Discussion in 'Mac Programming' started by vnraju, Aug 10, 2007.

  1. macrumors newbie

    vnraju

    #1
    hi all,
    is it possible to change the background color of a window?
    if it is possible how it is?
    any suggestion is appreciated..
     
  2. macrumors 68000

    pianoman

    #2
    EDIT: since it's his own code, this post was irrelevant.
     
  3. macrumors G4

    Eraserhead

    #3
    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.
     
  4. macrumors 601

    HiRez

    #4
    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?
     
  5. macrumors newbie

    vnraju

    #5
    yaaa its my own app...
    im trying to change using the code
    [myWindow setbackgroungColor:[NSColor blackColor]];
    but it....doesn't work....[/COLOR]
     
  6. macrumors 601

    HiRez

    #6
    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.
     
  7. macrumors newbie

    vnraju

    #7
    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.....
     
  8. macrumors G4

    Eraserhead

    #8
    See my post (#2) for how to do it.
     
  9. macrumors newbie

    vnraju

    #9
    i didn;t found............dat......
     
  10. Moderator emeritus

    kainjow

    #10
    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.
     
  11. macrumors 68000

    Fukui

    #11
    Change your window style to textured. Does that work now?
    Also, make sure to tell the [window display] after setting the color.
     

Share This Page