0

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);

}

And in view UI hierarchy: enter image description here

And run enter image description here

finally, it only red color, but the backgroundcolor of window is blue.

melody5417
  • 355
  • 2
  • 13
  • "I found all the views and window be alpha" That's not English. What does that mean? – El Tomato Jun 06 '16 at 06:41
  • Have you set the background color of the window? If so, to what? Does your view incorrectly return `YES` from an override of `-isOpaque`? Are you using vibrancy? Can you reproduce the problem with a new project with just this custom view? – Ken Thomases Jun 06 '16 at 06:47
  • @ElTomato sorry for my poor english, I means the top view shows the desktop directly, without the bottom imageview. – melody5417 Jun 06 '16 at 07:09
  • @KenThomases Yes, I reproduce it. But I still not know why. I will post my source code in my question. Thanks for help. – melody5417 Jun 06 '16 at 07:24
  • What happens if you comment out the whole `drawRect` method from your view implementation? Does it show the blue background then? – kender Jun 06 '16 at 07:33
  • @kender Yes, if I comment out the drawRect, it runs right. – melody5417 Jun 06 '16 at 07:37
  • And why don't you just set the background color of your view, instead of drawing on it on the lower level? Like it's shown here: http://stackoverflow.com/a/27954805/4172 – kender Jun 06 '16 at 07:44
  • I divide it in two subViews, because in my project the viewController should manages four subviews, and these subviews are for different function. And really, I want to know what's the reason. I think there must be some knowledge I did not know before. – melody5417 Jun 06 '16 at 07:49

1 Answers1

0

I change the

NSRectFill 

to

NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);

Then it turns right.

melody5417
  • 355
  • 2
  • 13