简体   繁体   中英

Objective-C tiled background image in NSview

I have 2 NSViews arranged side by side that have backgrounds made from tiled images. When I add constraints to the views so that they resize with the main window the tiled background image no longer displays and the background is just black.

What am I missing here?

#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

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

[super drawRect:dirtyRect];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM