Am trying to add an NSButton image to the far right of an NSTextField. Specifically it is going to be a button which launches an NSPopover (graphical date picker calendar). Right now all I'm trying to do is get the image button on the far right of my NSTextField.
I have subclassed NSTextField like this and here is my code:
@implementation myDateTextField {
- (void) awakeFromNib {
// my image button icon is 16px by 16px
NSRect buttonFrame = NSMakeRect(0.0f,0.0f, 16.0f, 16.0f);
NSButton *popoverButton = [[NSButton alloc] initWithFrame: buttonFrame];
popoverButton.buttonType = NSMomentaryChangeButton;
popoverButton.bezelStyle = NSInlineBezelStyle;
popoverButton.bordered = NO;
popoverButton.imagePosition = NSImageOnly;
[popoverButton setImage:[NSImage imageNamed:@"calendar_button.png"]];
[popoverButton.cell setHighlightsBy:NSContentsCellMask];
[self addSubview:popoverButton];
NSLog(@"awakeFromNib loaded.");
}
}
Within Interface Builder I have set the class my my NSTextField to be myDateTextField and I can confirm that awakeFromNib is being called when my window loads. My problem is the button is not appearing at all!
Any words of advice as to what I may be doing wrong? I realize the location of the button probably will be off within the text field but right now all I'm trying to do is get it to appear.
There's a typo in your code here (NSRect =
, get rid of the "="). Otherwise I ran this code and it worked fine as long as I had an actual image named "calendar_button.png" which fits in the space. Without the image, the code still runs but nothing shows up.
Try testing that the image is actually found:
NSImage *calImage = [NSImage imageNamed:@"calendar_button.png"];
if(calImage == nil){
NSLog(@"Failed to find calendar image!");
}
[popoverButton setImage:calImage];
You can also try setting popoverButton.bordered
to YES
to see where the button will appear.