Charles
Charles

Reputation: 9

Want to have a CALayer to display inside the window

I'm trying to get a CALayer with a black background to display. I've attached the AppDelegate as the delegate to the file's owner. The console reports correctly the "awaking from nib..." log statement. I however, just see a blank grey window (335x390 in size), with no black box drawn in the middle. What concept am I missing?

Thanks in advance for the help,

Charles

#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
@implementation AppDelegate

-(void)awakeFromNib;
{
  NSLog(@"awaking from nib...");
  [[window contentView] setWantsLayer:YES];

  layer = [CALayer layer];
  [layer setBounds:CGRectMake(0.0,0.0,100.0,100.0)];

   // center the animation layer
   [layer setPosition:CGPointMake([[window contentView] frame].size.width/2, [[window contentView]frame].size.height/2)];
   CGColorRef color = CGColorCreateGenericRGB(0., 0., 0., 1);
   [layer setBackgroundColor:color];
   CFRelease(color);

   [layer setBorderWidth:5.0f];

   [[[window contentView] layer] addSublayer:layer];      
}
@end

Upvotes: 0

Views: 690

Answers (1)

Brad Larson
Brad Larson

Reputation: 170319

The construction code for your custom layer looks correct, although a color with an RGBA of (1,1,1,1) will be white, not black.

I'd verify that your outlet to your window property is connected correctly (make sure it's non-nil), and that the contentView is non-nil. If those are looking good, you could try to provide your own custom layer for the contentView by doing something similar to the following:

[[window contentView] setLayer:[CALayer layer]];
[[window contentView] setWantsLayer:YES];

Upvotes: 1

Related Questions