I write a window and two subviews, the drawRect of top subview is like:
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(NSMakeRect(100, 100, 200, 200));
and the bottom subview is a image view. But when I run, I found all the views and window be alpha, the top subview will show the desktop with the color.
Anybody knows the reason? Thanks for the help.
The structure of my code is like this: windowController includes: window and viewcontroller. viewcontroller includes view1 and view2. view1 is a imageview. view2 I write nothing, just override the drawRect with the codes before.
I had wrote these before,but did not meet this problem, so I am really confused.
When I check the view UI hierarchy, I found it is correct.
I reproduced this problem in my test project, this is my test project:
In appDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
TestViewController *controller = [[TestViewController alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window setBackgroundColor:[NSColor blueColor]];
[self.window setContentView:controller.view];
}
In TestViewController
- (instancetype)initWithFrame:(NSRect)frame {
self = [super init];
if (self) {
TestView *view = [[TestView alloc] initWithFrame:frame];
self.view = view;
}
return self;
}
In TestView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(dirtyRect);
}
finally, it only red color, but the backgroundcolor of window is blue.