Hi everyone,
I have come across a rather strange problem in a project that I am working on involving custom views. I currently have one view called AccountSummaryView, which draws a gradient background. It also contains a subview called GraphView, which currently just draws some lines with NSBezierPath.
This is all working as expected and looks like this:
However, if I click away to another application, this happens to the view:
I have put log statements in, and it appears that the view is continuously redrawn with ever decreasing rectangles. Strangely, this only happens when I add the GraphView as a subview of AccountSummaryView, which I am doing like this:
If I remove the addSubview line, the redrawing does not occur. The AccountSummaryView drawRect looks like this:
I can't see why adding a subview like this causes such bizarre behaviour. If I do the same thing in IB i.e create a custom view set to AccountSummaryView, and drop a GraphView into it, then it works perfectly well. However, I wish to control the GraphView programatically, so I would rather create it in code.
If any of you Cocoa gurus could point me in the right direction I would be very grateful
I have come across a rather strange problem in a project that I am working on involving custom views. I currently have one view called AccountSummaryView, which draws a gradient background. It also contains a subview called GraphView, which currently just draws some lines with NSBezierPath.
This is all working as expected and looks like this:
However, if I click away to another application, this happens to the view:
I have put log statements in, and it appears that the view is continuously redrawn with ever decreasing rectangles. Strangely, this only happens when I add the GraphView as a subview of AccountSummaryView, which I am doing like this:
Code:
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
accountBalanceGraphView = [[GraphView alloc] initWithFrame:NSZeroRect];
}
[self addSubview:accountBalanceGraphView];
return self;
}
If I remove the addSubview line, the redrawing does not occur. The AccountSummaryView drawRect looks like this:
Code:
- (void)drawRect:(NSRect)rect
{
[self drawBackgroundGradientForRect:rect];
NSRect graphViewRect = NSMakeRect(0, 0, rect.size.width * 0.7, rect.size.height * 0.8);
graphViewRect.origin.x += (rect.size.width - graphViewRect.size.width - 10);
graphViewRect.origin.y += ((rect.size.height / 2) - (graphViewRect.size.height / 2));
[accountBalanceGraphView setFrame:graphViewRect];
}
I can't see why adding a subview like this causes such bizarre behaviour. If I do the same thing in IB i.e create a custom view set to AccountSummaryView, and drop a GraphView into it, then it works perfectly well. However, I wish to control the GraphView programatically, so I would rather create it in code.
If any of you Cocoa gurus could point me in the right direction I would be very grateful