0
votes

I have an NSButton subclass in an autolayout environment. The subclass implements mouseEntered: and mouseExited: methods, which should change the positioning of the button's image. On mouseEntered: it is changed to NSImageLeft (showing the title) and on mouseExited: it is changed to NSImageOnly (not showing the title).

Autolayout takes care of the resizing of the NSButton subclass in my view hierarchy, but it does not look smooth as it is not using CoreAnimation for resizing the button width according to whether the title is to be shown or not. How can I let the resizing happen with an animation?

2

2 Answers

0
votes

Open an animation group, enable implicit animations, change the button image position, and then force layout:

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
    context.allowsImplicitAnimation = YES;
    button.imagePosition = NSImageOnly;
    [button.window layoutIfNeeded];
} completionHandler:nil];
0
votes

I solved it by using the following methods:

- (void)mouseEntered:(NSEvent *)theEvent {
    NSSize titleStringSize = [self.title sizeWithAttributes:@{NSFontAttributeName:[NSFont     boldSystemFontOfSize:11.0]}];
    self.widthConstraint.animator.constant = titleStringSize.width+self.originalWidth+5.0;
}

- (void)mouseExited:(NSEvent *)theEvent {
    self.widthConstraint.animator.constant = self.originalWidth;
}

originalWidth and widthConstraint are properties of my NSButton subclass that are defined during awakeFromNib.