NSPopUpButton Active Image

时间:2011-01-10 07:37:29

标签: cocoa nspopupbutton

我想创建一个带有自定义活动图像的NSPopUpButton。我有两个图像,一个用于非活动,另一个用于活动。在界面构建器中,我设置了Image和Alt。 NSPopUpButton的图片。图像显示正确但当我单击按钮时,它显示标准的暗按钮状态而不是Alt。图像。

以下是界面构建器面板的屏幕截图:http://cl.ly/0D2c0Y2y0f1Z462d311X

如何设置NSPopUpButton以在单击时显示我的备用图像?

1 个答案:

答案 0 :(得分:5)

Apple Dev论坛的开发人员向我指出了正确的方向:https://devforums.apple.com/message/364824

以下是我提出的NSPopUpButtonCell的子类,它尊重IB中的替代图像:

- (void)drawImageWithFrame:(NSRect)cellRect inView:(NSView *)controlView{
    NSImage *image = self.image;
    if([self isHighlighted] && self.alternateImage){
        image = self.alternateImage;
    }

    //TODO: respect -(NSCellImagePosition)imagePosition
    NSRect imageRect = NSZeroRect;
    imageRect.origin.y = (CGFloat)round(cellRect.size.height*0.5f-image.size.height*0.5f);
    imageRect.origin.x = (CGFloat)round(cellRect.size.width*0.5f-image.size.width*0.5f);
    imageRect.size = image.size;

    [image drawInRect:imageRect
                 fromRect:NSZeroRect
                operation:NSCompositeSourceOver 
                 fraction:1.0f 
           respectFlipped:YES 
                    hints:nil];    
}
相关问题