4

を使用してMacアプリケーションで作業していますが、カスタムの背景画像がNSScrollView必要です。NSScrollViewカスタムdocumentViewNSViewサブクラスでこのコードを使用しました:

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

これにより、documentViewの背景としてパターン画像が表示されます。

しかし、Mac OS X Lionでは、NSScrollView可能な限りスクロールするとバウンドし、醜い空白が表示されます。空白も背景画像で覆われるようにするにはどうすればよいですか?

4

3 に答える 3

2

をオーバーライドする代わりにdrawRect:、スクロール ビューのsetBackgroundColor:メソッドを使用して、パターン イメージで作成した NSColor を渡します。

于 2011-09-22T18:22:44.723 に答える
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

そして+ (void)replaceClipViewInScrollView:(NSScrollView*)scrollView、NSScrollView インスタンスで呼び出します。

于 2012-06-14T10:13:33.790 に答える
0

drawRect コードを NSScrollView サブクラスに入れます。IB で、NSScrollView の代わりにカスタム サブクラスを使用するように NSScrollView を変更します。また、スクロール ビューの属性インスペクターで [背景を描画] のチェックを外してください。

于 2011-10-01T00:21:34.887 に答える