xBACP
xBACP

Reputation: 551

Custom NSToolbarItem Button Not Showing

I have two custom NSToolbarItems in the toolbar of the application. Each class has a NSButton within, where I setup the button and then set the toolbar item's view to the button (the stop button item for example):

@implementation RBSStopButtonToolbarItem

@synthesize button = _button;

-(id)initWithItemIdentifier:(NSString *)itemIdentifier
{
    self = [super initWithItemIdentifier:itemIdentifier];

    if(self)
    {
        // create button
        _button = [[NSButton alloc] init];

        // set the frame and bounds to be the same size
        //[_button setFrameSize:NSMakeSize(64.0, 64.0)];
        //[_button setBoundsSize:NSMakeSize(64.0, 64.0)];

        // button will not have a visible border
        [_button setBordered:NO];

        // set the original and alternate images...names are "opposite"
        [_button setImage:[NSImage imageNamed:@"StopButtonAlternateIcon"]];
        [_button setAlternateImage:[NSImage imageNamed:@"StopButtonIcon"]];

        // image position
        [_button setImagePosition:NSImageOnly];

        // set button type
        [_button setButtonType:NSMomentaryChangeButton];

        // button is transparent
        [_button setTransparent:YES];

        // set the toolbar item view to the button
        [self setView:_button];


    }
    return self;
}

I have an IBOutlet for each custom NSToolbarItem:

// toolbar item for start button
IBOutlet RBSStartButtonToolbarItem *_startButtonToolbarItem;

// toolbar item for stop button
IBOutlet RBSStopButtonToolbarItem *_stopButtonToolbarItem;

Yet I do not see the images in the custom view toolbar items: missing images for toolbar items The images are .icns type. The example I attempted to following is here: NSButton in NSToolbar item: click issue

Is there anyone with experience who can offer advice?

Upvotes: 0

Views: 2017

Answers (2)

jaydillyo
jaydillyo

Reputation: 316

I don't know why, but:

[NSToolbarItem initWithCoder:] is calling [NSToolbarItem setImage:] which is then calling [NSButton setImage:] on the button you have set as the toolbar item's view. This wipes out what you have done.

The example that you are referring to DOES NOT subclass NSToolbarItem.

I recommend that you also DO NOT subclass NSToolbarItem, and instead add a regular NSToolbarItem to the toolbar via interface builder and then in awakeFromNib find that toolbar item via its item identifier and set the button as its view.

I have verified that doing it this way works as expected.

Upvotes: 1

user1097185
user1097185

Reputation: 1058

I do not follow why your example doesn't work. But I have worked out the custom NSToolbarItem with my own way without even using NSToolbarDelegate.

My way is assuming you build your toolbar within a nib and not with code(mostly).

What I am doing is creating my own NSView in my nib with whatever I want in it. Then I drag this NSView into into my NSToolbar in my nib. xCode will automatically place your NSView inside an NSToolbarItem. You can then drag this custom NSToolbarItem into the default items and place it with whatever order you want(so you don't even need to place it by code).

The tricky part is to subclass NSToolbarItem and then within the awakeFromNib of this specific NSToolbarItem subclss you set it's view to the NSView underneath it. You would also need to refer the NSView into an IBOutlet * NSView within that subclass.

Here is the code of the subclass.

The header file:

#import <Cocoa/Cocoa.h>

@interface CustomToolbarItem : NSToolbarItem
{
    IBOutlet NSView * customView;
}

@end

The Obj-c file:

#import "CustomToolbarItem.h"

@implementation CustomToolbarItem

-(instancetype)initWithItemIdentifier:(NSString *)itemIdentifier
{
    self = [super initWithItemIdentifier:itemIdentifier];
    if (self)
    {
    }
    return self;
}

-(void)awakeFromNib
{
    [self setView:customView];
}
@end

I have also wrote a blog post about how I did this:

http://pompidev.net/2016/02/24/make-a-custom-nstoolbar-item-in-xcodes-interface-builder/

Upvotes: 0

Related Questions