aleclarson
aleclarson

Reputation: 19035

Check if a NSView is visible in its NSWindow

How can I know if my NSView is visible within its NSWindow?

It needs to account for scrolling superviews.

It does not need to know if the NSWindow is visible to the user.

Accounting for overlapping views would be "nice to have" but not required.

Upvotes: 2

Views: 1356

Answers (1)

aleclarson
aleclarson

Reputation: 19035

This should do it.

@interface NSView (Visibility)
- (BOOL)visibleInWindow;
@end

@implementation NSView (Visibility)

- (BOOL)visibleInWindow
{
  if (self.window == nil) {
    return NO;
  }

  // Might have zero opacity.
  if (self.alphaValue == 0 || self.hiddenOrHasHiddenAncestor) {
    return NO;
  }

  // Might be clipped by an ancestor.
  return !NSIsEmptyRect(self.visibleRect);
}

@end

Note: Overlapping views are not accounted for.

Upvotes: 3

Related Questions