9

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

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

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

4

1 回答 1

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];    
}
于 2011-02-21T09:32:42.023 回答