2011-01-10 41 views
9

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

这里是界面生成器面板的屏幕截图:http://cl.ly/0D2c0Y2y0f1Z462d311X

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

+1

注意`respect```应该是`YES` – 2011-01-20 14:53:45

+0

感谢您的提示:) – keegan3d 2011-02-04 07:34:49

回答

4

从苹果开发论坛开发者向我指出了正确的方向: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];  
}