Question

I have used IB to do that,and everything is okay,but when I wrote code to do that I don't know, how to get the same result with IB.

In IB, I set it like the screenshot, the image I set will has same size in the button but when I use code, the image only show a part that to said,
when I use IB to set,the image will stretch automatically but using code to do that,it is not stretch.

Could anyone find the difference between code and IB screenshot?

1) write code to create NSButton is like this:

NSButton *tempBtn = [[NSButton alloc] initWithFrame: NSMakeRect(200, 200, 20, 20)];
[tempBtn setButtonType: NSMomentaryPushInButton];
[tempBtn setBezelStyle: NSRoundedDisclosureBezelStyle];
[tempBtn setBordered: NO];
[tempBtn setImage: [NSImage imageNamed: @"Basketball"]];
[tempBtn setImagePosition: NSImageOnly];
[tempBtn setTarget: self];
[tempBtn setAction: @selector(clickBtn:)];
[self.window.contentView addSubview: tempBtn];
[tempBtn release];

2) set it in IB is like this: set in IB

the result is like this:

result screenshot

Was it helpful?

Solution

You shoud set the imageScaling of the cell related to the button:

[tempBtn.cell setImageScaling:NSImageScaleProportionallyDown];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top