子类化NSView具有透明背景 [英] Subclassing NSView to have a transparent background
问题描述
我正在创建一个应用,其中我需要一个带有透明PNG图像的透明NSView.问题是,我正在绘制的NSView带有灰色背景.我有它的子类化(如TransparentRectangleView),但不知道在drawRect中放入什么使其透明.
I am creating an app where I need to have a transparent NSView with a transparent PNG image inside. The problem is, the NSView I'm drawing has a gray background on it. I have it subclassed (as TransparentRectangleView) but don't know what to put in drawRect to make it transparent.
我已经重写了isOpaque方法以返回NO,但这似乎无济于事...
I have already overridden the isOpaque method to return NO but it doesn't seem to help...
或者,已经有一个类似于iPhone的UIImageView的子类NSView(只要我可以在其中添加子视图,我就需要在其中添加一些文本).
Alternatively, is there already a subclassed NSView that is similar to the iPhone's UIImageView (as long as I can add subviews inside, I need to add some text inside).
推荐答案
要使视图透明,只需用[NSColor clearColor]填充它即可.
To make a view transparent, simply fill it with [NSColor clearColor].
- (void)drawRect:(NSRect)rect {
[[NSColor clearColor] set];
NSRectFill(rect);
}
isOpaque的默认实现返回NO,因此,如果您要继承NSView而不是其他某个视图,则无需担心会覆盖它.
The default implementation of isOpaque returns NO, so if you are subclassing NSView and not some other view you don't need to worry about overriding it.
这篇关于子类化NSView具有透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!