Question

I subclassed a custom view which I dragged into a window in Interface Builder. As the mouse enters the bounds of the view, I would like the height of the view to change. My problem is the height change is upwards and not downwards. I tried flipping the coordinates of the view with (BOOL)isFlipped but it doesn't have any effect on the direction of the height change. Any help on how I can change the height in the downward direction?

#import "ViewA.h"

@implementation ViewA

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                                    options:(NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways)
                                                                      owner:self
                                                                   userInfo:nil];
        [self addTrackingArea:trackingArea];

    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] setFill];
    NSRectFill(dirtyRect);
}

- (BOOL)isFlipped {
    return YES;
}


- (void)mouseEntered:(NSEvent *)theEvent {

    NSRect rect = self.frame;
    rect.size.height = 120;
    self.frame = rect;
}

- (void)mouseExited:(NSEvent *)theEvent {

    NSRect rect = self.frame;
    rect.size.height = 90;
    self.frame = rect;
}

@end
Was it helpful?

Solution

This is because the co-ordinate system in Cocoa starts from the bottom left corner. i.e the co-ordinates (0,0) is at the bottom left corner of the screen/parent view. So the increment in y co-ordinate (height) would increase the size upwards. What you should do is as you increment the height, shift the origin downwards, For e.g. if you want to increase/decrease the frame height to 90;

CGFloat heightDiff = 90 -self.frame.size.height;

NSRect  rect = self.frame;

rect.size.height = 90;

rect.origin.y -= heightDiff;

self.frame = rect;

That should do it.

Override the parent view's isFlipped and not the method of the view itself for the expected behavior.

For instance if you are adding it to the content view of window, sub class the windows contentView and override its isFlipped method to return YES.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top