Xcode: Set the image to the button on the touchpad (for the new MacBook Pro)

How to set the image on the button on the touch panel (for the new MacBook Pro)? I tried the following code, but it does not work, if I run the following code, the button does not appear in the touch panel.

- (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 on the following two lines, I can see the button shown in the touch panel.

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

So what happened? How can I set the image on the button on the touch panel?

+5
source share
1 answer

It looks like your image width is too wide. Even when the image is reduced so that you display the whole image inside the button, the button width is set equal to the 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 the touch panel is not large enough for your button to display

+4
source

Source: https://habr.com/ru/post/1259596/


All Articles