问题
I'm trying to recreate the segmented control used in Xcode where the images are drawn with a blue tint.
For example in Interface Builder:
So far I have a custom NSSegmentedCell
overriding - (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
, but I'm stuck at how to draw the segment image (1) with the gray tint and (2) with the blue tint if it is selected.
Also, it looks like the images are drawn with a bezel or gradient effect. When I draw the segment image myself it is simply black (because I use template images).
How can I recreate the segment from above?
回答1:
Segmented Control - with on-state appearance
Use an NSSegmentedControl
object with style NSSegmentStyleTexturedRounded
and mode NSSegmentSwitchTrackingSelectAny
.
- Place a Segmented Control object in your toolbar or bottom bar
- In the Attributes pane of the inspector, set the style to Textured Rounded and the mode to Select Any.
- Be sure to provide an image for the control (in Interface Builder, select an image from the Image combo box in the Attributes inspector).
Toggle-Style Toolbar Button
The way in which this is achieved for single buttons differs slightly from that of segmented controls:
- Use an image with the name
IconTemplate
orTemplate
appended to its (original) name - Select the
NSTexturedRoundedBezelStyle
Button Style
↳ For example I have three .png
images in my resources, MiddlePanelIconTemplate.png, CardPanelIconTemplate.png, and ReturnPanelIconTemplate.png using Round Textured Button Cell Style.
⌘ OS X Human Interface Guidelines | Window-Frame Controls
回答2:
Answer provided by 'L'l worked ok for me, but appending IconTemplate to your image name it's not working on Xcode 7.3 anymore. Instead of that, you have to go to your Assets catalog, choose the image you want to use and on the right panel you have to select the Attributes Inspector and on the combo that says 'Render As' select 'Template Image'.
回答3:
To make an NSButton
programmatically (macOS 10.13 / Xcode 9) with Blue Tint when ON and not when OFF (like some of the buttons in Xcode toolbar) I do the following:
- Add PNG to asset catalog with transparent background and white line drawing (pdf with black lines also works).
- Set image in asset catalog to render as "template" (setting is in right sidebar).
- Set only 1 image (do not use alternate image) or will not work.
- bezelstyle = NSTexturedRoundedBezelStyle.
- buttontType = NSToggleButton.
- yourButton.cell.controlTint = NSBlueControlTint (on) or NSDefaultControlTint (off) according to state.
来源:https://stackoverflow.com/questions/24927874/how-to-draw-segmented-control-with-blue-tint-like-xcode