How to add a background image to an NSView

Unlike UIView, NSView does not have a background property – which makes it less obvious how to set either a colour or a background image to your bland grey windows.

Here’s how to do it:

First, create a subclass of NSView and assign it to the view you’d like to change in interface builder. When you create your class Xcode provides two methods for you, one of which is drawRect:dirtyRect. In it you can specify a colour and fill the view with it before calling super:

Now instead of a bland grey you’ll have a shining white.

If a solid colour isn’t what you’re after, create your own colorWithPatternImage and use that instead – therefore adding a background image to your NSView:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.