Lion中的NSScrollView背景图片

时间:2011-08-04 11:29:42

标签: cocoa osx-lion nsview nsscrollview

我正在使用NSScrollView处理Mac应用程序,我希望NSScrollView拥有自定义背景图片。我在自定义documentView NSView子类中使用了此代码:

- (void)drawRect:(NSRect)rect {
    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"wood.jpg"]] set];
    NSRectFill(rect);
}

显示图案图像作为documentView的背景。

但是现在在Mac OS X Lion中,当NSScrollView滚动得更远时,{{1}}反弹,显示出丑陋的白色空间。如何让白色空间也被背景图像覆盖?

3 个答案:

答案 0 :(得分:2)

使用滚动视图的drawRect:方法,而不是覆盖setBackgroundColor:,将您使用模式图像创建的NSColor传递。

答案 1 :(得分:1)

你应该使用NSScrollView setBackgroundColor子类,但是你应该像这样子类化NSClipView来将纹理原点固定到顶部:

@implementation MYClipView

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

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
  if (self.drawsBackground)
  {
    NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
    [theContext saveGraphicsState];

    float xOffset = NSMinX([self convertRect:[self frame] toView:nil]);
    float yOffset = NSMaxY([self convertRect:[self frame] toView:nil]);
    [theContext setPatternPhase:NSMakePoint(xOffset, yOffset)];
    NSColor* color = self.backgroundColor;
    [color set];
    NSRectFill([self bounds]);
    [theContext restoreGraphicsState]; 
  }  
  // Note: We don't call [super drawRect:dirtyRect] because we don't need it to draw over our background. 
}

+ (void)replaceClipViewInScrollView:(NSScrollView*)scrollView 
{
  NSView* docView = [scrollView documentView]; //[[scrollView documentView] retain]; 
  MYClipView* newClipView = nil;
  newClipView = [[[self class] alloc] initWithFrame:[[scrollView contentView] frame]]; 
  [newClipView setBackgroundColor:[[scrollView contentView] backgroundColor]];
  [scrollView setContentView:(NSClipView*)newClipView]; [scrollView setDocumentView:docView];
//  [newClipView release]; 
//  [docView release]; 
}
@end

使用NSScrollView实例调用+ (void)replaceClipViewInScrollView:(NSScrollView*)scrollView

答案 2 :(得分:0)

将drawRect代码放在NSScrollView子类中。在IB中,更改NSScrollView以使用您的自定义子类而不是NSScrollView。另外,请确保在滚动视图的属性检查器中取消选中“绘制背景”。

相关问题