WrightsCS
WrightsCS

Reputation: 50727

Transparent NSImageView falls behind NSView

I have a transparent .png image that lays over an NSView and it should look like this:

enter image description here

However, sometimes, with no rhyme or reason or indication that it is going to do this, the NSImageView falls behind the NSView:

enter image description here

I am not sure how to keep the NSImageView above the NSView, or why it even falls behind the NSView.

Here is what it looks like in Interface Builder: enter image description here

Update:

I have tried the following with same results:

[header removeFromSuperview];
[mainView addSubview:header positioned:NSWindowAbove relativeTo:nil];

What is a better way to achieve this effect?

Upvotes: 1

Views: 1154

Answers (3)

WrightsCS
WrightsCS

Reputation: 50727

Turns out that I had to re-arrange some of the views in Interface Builder, then in code:

[[header layer] setZPosition:1.0f];

Upvotes: 2

diederikh
diederikh

Reputation: 25281

When you have two overlapping NSView on the same level it is undefined what the drawing order will be. You can sort the drawing of the subviews using SubviewsUsingFunction:context:. You'll have to provide a comparison function that compares based on some value (e.g. tag):

NSComparisonResult viewCompareByTag(NSView *firstView, NSView *secondView, void *context) {
    return ([firstView tag] > [secondView tag]) ? NSOrderedAscending : NSOrderedDescending;
}

[mySuperView sortSubviewsUsingFunction:&viewCompareByTag context:nil];

Upvotes: 2

Skyler Saleh
Skyler Saleh

Reputation: 3991

Cocoa UI elements that overlap have an undefined drawing order, and Apple states that you shouldn't overlap them. You could create some custom drawing code to make your current design work. However, honestly I would change the design so it matches with Mac OS X's human interface guidelines. You can read about these at the link below. http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGIntro/XHIGIntro.html%23//apple_ref/doc/uid/TP30000894-TP6

Upvotes: 2

Related Questions