Xcode:将图像设置为触控条上的按钮(适用于新MacBook Pro)

RRN*_*RRN 5 macos xcode cocoa objective-c

如何将图像设置为触控条上的按钮(适用于新MacBook Pro)?我尝试了以下代码,但它不起作用,如果我运行以下代码,触摸栏中没有显示任何按钮.

  - (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;
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉以下两行,我可以看到触摸栏中显示的按钮.

    [theButton setImage:[NSImage imageNamed:@“scan.png”]];
    [theButton setImagePosition:NSImageLeft]; 
Run Code Online (Sandbox Code Playgroud)

那有什么不对?如何将图像设置为触控条上的按钮?

小智 4

您的图像宽度似乎太大了。即使缩小图像以在按钮内显示整个图像,按钮宽度也会设置为等于原始图像宽度。

128x128: 在此输入图像描述

256x256:在此输入图像描述

512x512:在此输入图像描述

1024x1024:无按钮 - 因为触摸栏的可用尺寸不足以显示按钮