Making an NSButton resize its image (Cocoa OSX) - cocoa

I could not find a way in the documentation to tell an NSButton to resize its image to fill up the whole button. Is there a way to do this programatically?

The closest you'll get is -setImageScaling: ... look up the constants to see how the image will be scaled within the button cell, given its bordered state and bezel type.
If you're looking to replace the standard button entirely with your image (ie, the button cell doesn't draw itself at all - your image serves as the entire visual representation), turn off the border (-setBordered:).
All of these options can be configured in IB as well. A tip: in IB, hover the mouse over any setting in the inspector panel - most if not all give you a hint that shows what method controls the behavior affected by the setting's control.

Related

Xcode invisible elements

I don't know what happened. Elements items like buttons or segmented control are invisible in main storyboard. Cellphone is the same.
I put the image, of what I mean
It looks like you accidentally changed the view's (or window's) Tint Color to white. (Or you could even have turned it to Clear color, or no color at all.)
This would cause the text of the button or segmented control to be invisible, because it is white on white. The segmented control has a border but it would turn white too. So you wouldn't be able to see anything.
There are two places to look to fix this. Look at the Tint popup of your view controller's View in the Attributes inspector.
Also look at the Global Tint popup in the File inspector for the whole storyboard.
[EDIT It turned out to be the Global Tint.]

Confused with NSButton Image

I have a square NSButton and an Image the same size of the button. I set the image to the button, turned off bordered and set scaling to none. Then the image seems smaller than the button. If I set scaling to axes independent, the image can fill the all button. But is set button enable to false. The button disappeared. Does anyone know what's wrong with it?
Well... Your description, IMHO, is a bit confused! Anyway, for what I understood, you're having problem with Xcode.
You have to set the scaling because the button is always a bit larger than its content, because of the border (even if you turn off its borders, it will still be rendered with a small invisible border). For an unknown reasons, setting to Axes Independently solves this problem and resizes the image in the correct way (this works in any kind of NSControl you can use).
I cannot figure out how enabling or disabling the button can influences its visibility. Are you sure you're not using the "Hidden" property instead of the "Enabled" one?
[myButton setHidden:YES]
This will make the button transparent and insensible to clicks.
[myButton setEnabled:NO]
This will act on the answer to user clicks (and it's what you're looking for). Is your image mainly grey? Disabling the button will make the button gray, and this could make the image invisible...

NSButton - Gray rectangle whenever the custom button is clicked

Can I just want to replace the appearance by setting a property of NSButton?
I am able to change the appearance by using the "image" property of the button but I got an ugly gray rectangle whenever the custom button is clicked (the image has a transparency). Is there a way to hide that rectangle?
Thanks.
The following call changes the behavior by making image be darkened when clicked:
[[button cell] setHighlightsBy:NSContentsCellMask];

Can't Edit NSWindow's Toolbar - Cocoa

So I'm using the method:
[someWindow setContentBorderThickness:24.0 forEdge:NSMaxYEdge];
But I can't seem to get the toolbar to increase in height. It simply stays the same as in default. Can anyone shed some light here?
An NSToolbar is automatically resized to accommodate the height of the tallest NSToolbarItem. The standard (large) toolbar items are all 32 px tall, so the toolbar has no need to make itself larger. If you do something like add a custom view toolbar item, then it will be resized to accommodate that item, as shown in the image below:
(To accomplish the result shown above, I clicked on the toolbar twice in IB to bring down the Allowed Toolbar Items sheet, then dragged an NSView custom view from the library palette onto that sheet).
P.S. I'd recommend using this capability with discretion.
You cannot specify an arbitrary height for NSToolbar. You can, however, specify a size mode. A toolbar with 24x24-pixel icons has a small size mode:
[toolbar setSizeMode: NSToolbarSizeModeSmall];
which is equivalent to Size: Small in Interface Builder’s Attributes Inspector.

How can I change the way NSButtonCell objects highlight when clicked?

I am using several NSButtonCell objects in an NSTableView. They are simple square buttons with custom images on them.
These buttons draw properly when they are not highlighted: all that is visible is the image, and the rest of the button rectangle is transparent. However, when I click on them, the entire button rectangle is highlighted, inverting the background in the parts that were transparent.
I would prefer to see the image drawn inverted, and the transparent parts remain transparent. How can this be done?
Try setting your cell's highlightsBy property to NSContentsCellMask. I think you'll have to do this in code (probably in awakeFromNib); I don't see a way to do it in IB alone.
You can do it in Interface Builder too. I use "Square Button" so the button alters between two images (so the image is not inverted at all).
Your buttons behaviour is probably set to "Momentary Light" or "Momentary Push In".
Set the Behaviour to "Momentary Change", and it should work.

Resources