Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Set Image to a Button on Touch Bar (for new MacBook Pro)

Tags:

How can I set an Image to a Button on Touch Bar (for new MacBook Pro)? I tried the following code, but it doesn't work, if I run the following code, no button shown up in the Touch Bar.

  - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier 
{

if ([identifier isEqualToString:TouchBarMacScanIdentifier])
{
    NSButton* theButton = [NSButton buttonWithTitle:@"Scan" target:self action:@selector(clickFullScan:)];

    [theButton setImage:[NSImage imageNamed:@“scan.png”]];
    [theButton setImagePosition:NSImageLeft]; 

    NSCustomTouchBarItem *customItemForButton =
    [[NSCustomTouchBarItem alloc] initWithIdentifier:TouchBarMacScanIdentifier];

    customItemForButton.view = theButton;

    customItemForButton.visibilityPriority = NSTouchBarItemPriorityLow;

    return customItemForButton;
}

...

return nil;
}

If I comment out the following two lines, I can see the button shown up in the Touch Bar.

    [theButton setImage:[NSImage imageNamed:@“scan.png”]];
    [theButton setImagePosition:NSImageLeft]; 

So what's wrong? how can I set an Image to the Button on Touch Bar??

like image 545
RRN Avatar asked Nov 11 '16 16:11

RRN


1 Answers

It looks like your image width is too big. Even when the image is downscaled for you to show whole image inside of the button, the button width is set to be equal to your original image width.

128x128: enter image description here

256x256:enter image description here

512x512:enter image description here

1024x1024: No Button - since the free size of touch bar is not big enough for your button to display

like image 163
Roman Roba Avatar answered Sep 26 '22 14:09

Roman Roba