I have a problem setting the background of my subclassed NSView called TitlebarView
. I want to use an image as a background via colorWithPattternImage
, but the result is a black background not the image i gave the method as a parameter.
Here's my code of TitlebarView
:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSString* pathToTitlebarBGImage = [[NSBundle mainBundle] pathForResource:@"titlebar_bg" ofType:@"png" inDirectory:@"Resources/gui_images"];
NSLog(@"path to titlebar bg image: %@", pathToTitlebarBGImage);
// NSString* pathToTitlebarBGImage = @"Resources/gui_images/titlebar_bg.png";
self.titlebarBGImage = [NSImage imageNamed:pathToTitlebarBGImage];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
[[NSColor colorWithPatternImage:self.titlebarBGImage] setFill];
// [[NSColor redColor] setFill];
NSRectFill(dirtyRect);
}
titlebarBGImage
is a properly set property in the header files.
If I use the line with redColor
I get the red colored view, so the code is working to some degree. For all I could find in the documentation an on stackoverflow this should actually work as intended. What am I missing here?
This is isolated problem of my overall question found here
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…