0

我有 2 个并排排列的 NSView,它们的背景由平铺图像制成。当我向视图添加约束以便它们使用主窗口调整大小时,平铺背景图像不再显示并且背景只是黑色。

我在这里想念什么?

#import "imageWellGraphics.h"

@implementation imageWellGraphics

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
    CGAffineTransform affineTransform = CGContextGetCTM(context);

    NSImage* image = [NSImage imageNamed: @"tile.png"];
    NSColor* imagePattern = [NSColor colorWithPatternImage: image];

    NSRect frame = NSInsetRect(self.bounds, 1, 1);

    NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRect:NSMakeRect(NSMinX(frame), NSMinY(frame), NSWidth(frame), NSHeight(frame))];
    [NSGraphicsContext saveGraphicsState];
    CGContextSetPatternPhase(context, NSMakeSize(affineTransform.tx, affineTransform.ty));
    [imagePattern setFill];
    [rectanglePath fill];
    [NSGraphicsContext restoreGraphicsState];
}

@end
4

2 回答 2

0

将此行包含在drawrect方法中并检查:-

[super drawRect:dirtyRect];
于 2013-10-31T05:34:10.290 回答
0

好吧,事实证明问题是我没有正确理解/使用 Images.xcassets 作为我的背景图像。

于 2013-11-07T07:41:05.180 回答