2016-11-11 11 views
5

タッチバーのボタンに画像を設定するにはどうすればよいですか? 次のコードを試しましたが、次のコードを実行すると、タッチバーに表示されたボタンが表示されません。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; 
} 

次の2行をコメントアウトすると、タッチバーに表示されているボタンが表示されます。

[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:いいえボタン - タッチバーの空きサイズが

を表示するには、ボタンのための十分な大きさではありませんので、
関連する問題