我想创建一个带有自定义活动图像的NSPopUpButton。我有两个图像,一个是非活动的,另一个是活动的。在界面生成器中,我设置了Image和Alt。NSPopUpButton的图像。图像显示正确,但当我单击按钮时,它会显示标准的深色按钮状态,而不是Alt。图像。
下面是界面构建器面板的屏幕截图:http://cl.ly/0D2c0Y2y0f1Z462d311X
如何将NSPopUpButton设置为在单击时显示我的备用图像?
发布于 2011-02-21 17:32:42
来自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];
}https://stackoverflow.com/questions/4644612
复制相似问题