1
votes

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.

1

1 Answers

3
votes

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.