9

I would like to be notified whenever a certain NSView's - (NSRect)visibleRect changes because I want to do some fancy subview layout based on the visible rect. Frankly, right now I'm stumped; -visibleRect doesn't emit KVO notifications (which makes sense), and there doesn't seem to be way to find out if the visible rect changed or not without explicitly calling -visibleRect.

Is this at all possible? (or is it a terrible, terrible idea?)

Vervious
  • 5,559
  • 3
  • 38
  • 57

4 Answers4

6

I think you can either override -[NSView updateTrackingAreas] or listen for NSViewDidUpdateTrackingAreasNotification. Those may happen on more occasions than just a change of the visible rect, but they should happen for any change of the visible rect. I think.

That said, it may be a terrible idea. Hard to know. :)

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Good question and answer. This answer helped me out of a different problem I was facing but I have this question as well. And for this question I would up vote this answer but not accept it. – trss Aug 23 '12 at 07:04
  • The docs for `updateTrackingAreas` says "Invoked automatically when the view’s geometry changes such that its tracking areas need to be recalculated." but it's not clear what the view's geometry means. There is a topic on View Geometry but that only explains bounds and frame. It does explain visibleRect but it doesn't call it as the view's geometry. – trss Aug 23 '12 at 10:24
  • I guess it is called when either the frame, bounds, visible rectangle or the transformation on the view changes as someone on the #macdev irc channel explained. – trss Aug 23 '12 at 12:09
1

Another option post 10.5 is the -viewWillDraw method which is called just prior to the view (and its subviews) being drawn. You can fetch the view's visible rectangle and perform layout prior to calling [super viewWillDraw].

MrO
  • 685
  • 5
  • 8
0

Ken's suggestions of listening for the tracking area changes feels hacky but seems to work, although they only trigger after the resize is complete. If you need updates during resizing like I did, it overriding -[NSView resizeWithOldSuperviewSize:] will do that

iain
  • 5,660
  • 1
  • 30
  • 51
0

The adjustTrackingArea solution does not appear viable in Mojave for NSScrollView at least. Mojave does not appear to always call adjustTrackingArea while scrolling an NSScrollView. Haven't tested other OS versions, other view types.

Keith Knauber
  • 752
  • 6
  • 13