【问题标题】:NSScrollView background image in LionLion中的NSScrollView背景图像
【发布时间】:2011-08-04 11:29:41
【问题描述】:

我正在使用NSScrollView 开发一个Mac 应用程序,并且我希望NSScrollView 具有自定义背景图像。我在自定义documentViewNSView子类中使用了这段代码:

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

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

但现在在 Mac OS X Lion 中,NSScrollView 在滚动得超出可能范围时会弹跳,显示出丑陋的空白。如何让空白也被背景图片覆盖?

【问题讨论】:

  • 嗨!你子类化了哪个类来覆盖这个方法?后面的图片怎么发?附言。您的背景滑动是否正确并固定在顶部?

标签: cocoa osx-lion nsview nsscrollview


【解决方案1】:

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

【讨论】:

  • 谢谢,但这对我不起作用,因为背景不会滚动。它固定在 ScrollView 上。
【解决方案2】:

你应该使用 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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多