转自:http://blog.csdn.net/joddy2005/article/details/6183440
A button can have two images associated with it: normal and alternate. If the button type isNSMomentaryPushInButton
, NSPushOnPushOffButton
, NSMomentaryLightButton
,
or NSOnOffButton
,
only the normal image is ever displayed. If the button type is NSMomentaryChangeButton
orNSToggleButton
,
the normal image is displayed when the button’s state is off (NSOffState
)
and the alternate image is displayed when the button’s state is on or mixed (NSOnState
.
or NSMixedState
).
If you want a button to display different image for all three states, you must subclass NSButton
. (Although switch and radio buttons can display different images for all three states, there is no public interface for this feature.)
To set the normal image, use setImage:
. To set the alternate image, use setAlternateImage:
.
Note: If a button is a checkbox or a radio button, do not change its images. The images for these buttons are system-defined and changing them could lead to unpredictable results. If you want a switch or radio button with a customized appearance,
either customize a toggle button (a button whose type is NSToggleButton
)
or subclass NSButton
.
Example:
NSButton *btn = [[NSButton alloc] initWithFrame: frame];
btn.setImage:[UIImage imageWithContentOfFile:path];
btn.setAlternateImage:[UIImage imageWithContentOfFile:path1];
Open the XIB view. Double click on the view (it will be a xib file) to open it up.
Add UIButton. Drag an UIButton from the library onto your view.
Change the button’s type to custom. Select the UIButton and then use the attributes applet to change the button type to “custom”.
Add images to button. Use the drop down box a near the top of the applet to change the attributes for each button state. Assign a different image to correspond to each state.
To set the position for a button’s image, use setImagePosition:
, with one of the following values below. The default is NSNoImage
|
||