Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

NSWindow with image background?

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

User uploaded file

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD

Posted on Feb 3, 2013 10:09 AM

Reply
13 replies

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:


propertymainWindow : 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):


setmyBundletocurrent application's NSBundle's mainBundle()

setbackgroundImagetocurrent 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))

Feb 3, 2013 3:06 PM in response to red_menace

got error:

-[AppDelegate applicationWillFinishLaunching:]: The variable mainWindow is not defined. (error -2753)



on applicationWillFinishLaunching_(aNotification)


-- Insert code here to initialize your application before any files are opened

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

set backgroundImage to current application's NSImage's alloc's initWithContentsOfFile_(myBundle's pathForResource_ofType_("plex", "png"))

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

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

end applicationWillFinishLaunching_

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).

NSWindow with image background?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.