Cocoa 在Lion中查看背景图像
我正在使用带有Cocoa 在Lion中查看背景图像,cocoa,osx-lion,nsview,nsscrollview,Cocoa,Osx Lion,Nsview,Nsscrollview,我正在使用带有NSScrollView的Mac应用程序,我希望NSScrollView具有自定义背景图像。我在自定义documentViewNSView子类中使用了此代码: - (void)drawRect:(NSRect)rect { [[NSColor colorWithPatternImage:[NSImage imageNamed:@"wood.jpg"]] set]; NSRectFill(rect); } 将图案图像显示为documentView的背景的 但现在在M
NSScrollView
的Mac应用程序,我希望NSScrollView
具有自定义背景图像。我在自定义documentViewNSView
子类中使用了此代码:
- (void)drawRect:(NSRect)rect {
[[NSColor colorWithPatternImage:[NSImage imageNamed:@"wood.jpg"]] set];
NSRectFill(rect);
}
将图案图像显示为documentView的背景的
但现在在Mac OS X Lion中,
NSScrollView
在超出可能的滚动范围时会反弹,显示难看的空白。如何使空白也被背景图像覆盖?不要覆盖drawRect:
,而是使用滚动视图的setBackgroundColor:
方法,传递用图案图像创建的NSColor。将drawRect代码放入NSScrollView子类中。在IB中,将NSScrollView更改为使用自定义子类而不是NSScrollView。另外,请确保取消选中滚动视图属性检查器中的“绘制背景”。您应该使用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)replaceClipViewWinScrollView:(NSScrollView*)scrollView
。谢谢,但它对我不起作用,因为当时背景不会滚动。它固定在滚动视图上。嗨!您使用了哪个子类来重写此方法?如何发送背面的图像?另外,你的背景滑动是否正确并固定在顶部?