2016-11-11 46 views
5

如何將圖像設置爲觸控板上的按鈕(用於新MacBook Pro)? 我嘗試了下面的代碼,但它不起作用,如果我運行下面的代碼,沒有按鈕出現在觸摸欄中。Xcode:將圖像設置爲觸控板上的按鈕(適用於新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; 
} 

如果我註釋掉以下兩行,我可以看到觸摸欄中顯示的按鈕。

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

那麼,怎麼了?如何將圖像設置爲觸摸欄上的按鈕?

回答

4

它看起來像你的圖像寬度太大。 即使縮小圖像以顯示按鈕內部的整個圖像,按鈕寬度也會設置爲等於原始圖像寬度。

128×128: enter image description here

256×256:​​

512×512:enter image description here

1024×1024:沒有按鈕 - 因爲觸摸條的可用空間不夠大你的按鈕顯示

相關問題