You usually configure most aspects of a window’s appearance in Interface Builder. Sometimes, however, you may need to create a window programmatically, or alter its appearance after it has been created.
Setting a Window’s Style
Setting a Window’s Color and Transparency
The peripheral elements that a window displays define its style. Though you can’t access and manipulate them directly, you can determine at initialization whether a window has them by providing a style mask to the initializer. There are four possible style elements, specifiable by combining their mask values using the C bitwise OR operator:
Element | Mask Value |
---|---|
A title bar | |
A close button | |
A minimize button | |
A resize bar, border, or box |
You can also specify NSBorderlessWindowMask
, in which case none of these style elements is used.
Typically you set a window’s appearance once, when it is first created. Sometimes, however, you want to enable or disable a button in the title bar to reflect changed context. To do this you first retrieve the button from the window using the NSWindow standardWindowButton:
method and then set its enabled state, as in the following example.
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; |
[closeButton setEnabled:NO]; |
The constants required to access standard title bar widgets are defined in the API reference for NSWindow
.
You can set a window’s background color and transparency using the methods setBackgroundColor:
and setAlphaValue:
, respectively.
You can set a window’s background color to a nonopaque color. This does not affect the window’s title bar, and only makes the background itself transparent if the window itself is not opaque, as illustrated in the following example.
[myWindow setOpaque:NO]; // YES by default |
NSColor *semiTransparentBlue = |
[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.5]; |
[myWindow setBackgroundColor:semiTransparentBlue]; |
Views placed on a nonopaque window with a transparent background color retain their own opacity. If you want to make the entire window—including the title bar and views placed on the window—transparent, you should use setAlphaValue:
.
© 2002, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-02-04)