Skip navigation

NSWindow with image background?

811 Views 13 Replies Latest reply: Feb 3, 2013 4:14 PM by MacMan240 RSS
MacMan240 Level 1 Level 1 (80 points)
Currently Being Moderated
Feb 3, 2013 10:09 AM

I am making a Cocoa Application I would Like to get an image to be in the background without using an image well.

Screen Shot 2013-02-03 at 1.07.05 PM.png

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD
  • xnav Level 5 Level 5 (6,625 points)
    Currently Being Moderated
    Feb 3, 2013 11:18 AM (in response to MacMan240)

    You could use a WebView that just loads a jpeg.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 12:24 PM (in response to MacMan240)

    You can also directly set the window background to an image, depending on whether your window is resizable or if the image is some kind of pattern.  The image will be tiled if it is smaller than the window, so if the window is a fixed size and the image is not a pattern you can to resize it to match the window.

     

    The following example uses an outlet property that is connected to the main window (not the view), so place it with the rest of your outlet properties:

     

    property mainWindow : missing value -- outlet connected to the main window

     

    These statements should be placed somewhere with the rest of your initialization code, perhaps in the applicationWillFinishLaunching handler.  I am also guessing that your image will be in the Resources folder of the application bundle (make sure you have it added to the Copy Bundle Resources build phase):

     

    set myBundle to current application's NSBundle's mainBundle()

    set backgroundImage to current application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("image name", "jpg"))

    backgroundImage's setSize_({350, 230}) -- match window size

    mainWindow's setBackgroundColor_(current application's NSColor's colorWithPatternImage_(backgroundImage))


  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 3:11 PM (in response to MacMan240)

    To add the image to your project, right click in one of your group folders (Supporting Files, for example), select the Add Files to... menu item and choose the file.  It should also appear in the Copy Bundle Resources phase in the project Build Phases.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 3:12 PM (in response to MacMan240)

    You need to add the property declaration and connect it to the main window.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 3:45 PM (in response to MacMan240)

    In the Interface Editor, if you select the blue cube that represents your AppDelegate instance and look at the Connections Inspector, all of the Outlets and Received Actions will be listed.  When you add a property that is set to missing value (it is just a placeholder), it will show up in the outlets, just as a handler with a single parameter will show up in the received actions.

     

    The various UI objects are connected to outlet properties in the same way you connect to the buttons - once connected, the property is used as a reference to the UI object (things get connected up and the property is given an actual value the application is run).

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 3:51 PM (in response to MacMan240)

    Make sure you connected the property to the window and not the view - errors will get logged to Xcode's debug console, so you can check to see what is (not) happening.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 3, 2013 4:04 PM (in response to MacMan240)

    I chose the pattern image since it is fairly simple.  To get more control over the background, you would be looking at subclassing NSView or adding some view layers in there.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.