- (void)drawRect:(NSRect)dirtyRect
{
if ([NSApp isActive]) {
[[NSColor redColor] set];
}
else {
[[NSColor blueColor] set];
}
NSRectFill([self bounds]);
}
pretty simple stuff you would think but if I drop this custom view onto the main window and run it up the view draw a blue square??? how bizarre. If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here? I can only think the whole view is being clipped as the system doesn't think it needs to be redrawn as the very first drawrect the app is showing as inactive but then subsequently drawrect gets called again with the app in an active state so it initially draws blue but then is supposed to draw red but that's not happening.
Any help would be appreciated.
Thanks
_______________________________________________
Cocoa-dev mailing list (Coco...@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com
This email sent to cocoa-dev-ga...@googlegroups.com
> Hi all
> This is must be the most simple a puzzling problem I've had. Take a new app, create a custom view class with a drawrect of
>
> - (void)drawRect:(NSRect)dirtyRect
> {
> if ([NSApp isActive]) {
> [[NSColor redColor] set];
> }
> else {
> [[NSColor blueColor] set];
> }
> NSRectFill([self bounds]);
> }
>
> pretty simple stuff you would think but if I drop this custom view onto the main window and run it up the view draw a blue square??? how bizarre. If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here? I can only think the whole view is being clipped as the system doesn't think it needs to be redrawn as the very first drawrect the app is showing as inactive but then subsequently drawrect gets called again with the app in an active state so it initially draws blue but then is supposed to draw red but that's not happening.
> Any help would be appreciated.
> Thanks
The problem is, I think, that the view is not automatically invalidated when the app is inactivated or activated. Therefore what you see is whatever it was in its previous state. You need to listen for the activation/inactivation notification, and invalidate the view.
--Graham
> If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here?
Activating/deactivating an app does not force all of its views to redraw! That would be really expensive. If you want your view to change appearance when this happens, you'll have to listen for the right notification and call -setNeedsDisplay:. That's what controls that update their appearance when active/inactive do.
Also, you probably don't want the appearance to be based on whether the app is active or inactive; that would be rather weird and nonstandard. Instead, base it on whether the window is key.
—Jens