inessential by Brent Simmons

First Go at Using IB_DESIGNABLE and IBInspectable

In AppKit, views don’t have a backgroundColor property. This isn’t a great big pain or anything — just a small thing. But nice to solve.

There are many times when I have a view that contains other views and the container view should have a background color. (And be opaque. I’m a big fan of opaque views.)

And sometimes I just need a divider — and a divider is, after all, just a thin view with a background color.

And I’d really love it if I could set the colors in Interface Builder, and I’d love it if IB actually drew the correct background colors. That would go a surprisingly long way toward making IB a WYSIWYG editor.

So here’s what I did:

Created BackgroundColorView.

In the .h file:

IB_DESIGNABLE
@interface BackgroundColorView : NSView

@property (nonatomic) IBInspectable NSColor *backgroundColor;

@end

In the .m file:

- (BOOL)isOpaque {

  return YES;
}

- (void)drawRect:(NSRect)r {

  [self.backgroundColor setFill];
  NSRectFill(r);
}

And then in IB I just use a BackgroundColorView (I set the Custom Class to BackgroundColorView), and it does what I want. I can set the color in IB, and IB draws the correct background color.

I like it. It means that I’m not the bottleneck — designers can change the background color themselves. Cool.