如何绘制NSView的子视图

时间:2011-03-31 09:33:33

标签: objective-c cocoa nsview subview drawrect

我正在尝试在NSView的顶部绘制,其中包含一些子视图。 实际上我正在尝试重现Interface Builder的连接线样式。这是我目前正在使用的代码:

- (void)drawRect:(CGRect)dirtyRect 
{
    // Background color
    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);

    // Draw line
    if(_connecting)
    {
        CGContextRef c = [[NSGraphicsContext currentContext] graphicsPort];
        [[NSColor redColor] setStroke];

        CGContextMoveToPoint(c, _start.x, _start.y);
        CGContextAddLineToPoint(c, _end.x, _end.y);        
        CGContextSetLineWidth(c, LINE_WIDTH); 
        CGContextClosePath(c);
        CGContextStrokePath(c);
    }
}

第一部分是为我的NSView着色(如果你知道另一种方式,请告诉我'因为我来自iPhone开发而我错过了backgroundColor的{​​{1}}属性

然后,如果检测到连接,我用2 UIView s绘制它。此代码有效,但我没有在第一个NSPoint上绘制子视图。

1 个答案:

答案 0 :(得分:10)

父视图无法绘制其子视图。您必须在子视图上放置另一个视图并在那里绘制线条。