views:

20

answers:

2

Within my Mac app I've setup two NSSplitViews to create a Mail-style interface (with the first ScrollView used to create a vertical separator, and the second nested within the first, to create the horizontal separator). To illustrate the interface, I've created the following diagram:

alt text

To illustrate the setup further, here's an annotated snap of my view hierarchy:

alt text

The issue I'm having is that, when trying to grab the frames of two NSTableView's hosted within portion B and C of my NSSplitView, I receive relative coordinates. Here's the code I'm using:

NSRect aTableViewFrame = [aTableView frame];
NSRect bTableViewFrame = [bTableView frame];
NSLog(@"%@ %@", NSStringFromRect(aTableViewFrame), NSStringFromRect(aTableViewFrame));

Here's the output I see:

{{0, 0}, {760, 143}} {{0, 0}, {760, 392}}

As you can see from the returned NSRect values, I'm given the correct width and height of the NSTableView, but what I'm really after is their position relative to the parent NSSplitView. Of course, I could use (a number of) IBOutlets and construct the correct rect value myself, but what I'm really looking for is an easy way to grab the frame of the NSTableViews relative to either the parent NSSplitView or the containing Window.

Any ideas?

A: 

The reason you're getting those values is because the NSTableViews are not direct subviews of the NSSplitView. Firstly, an NSTableView is actually enclosed within an NSScrollView, and according to your IB screenshot, that is in turn enclosing within another plain NSView (the "Custom View" you see in the outline there).

If it were me, I would either go the route of just making an outlet to the direct subviews of the split view, or access them by just calling -subviews on the split view, rather than trying to navigate up the hierarchy from the table view. Either of those options seems less fragile than working upwards from the table view, in my opinion.

However, if you want to go from the table view to the subview of the split view, this should do the trick:

NSRect aTableViewFrame = [[[aTableView enclosingScrollView] superview] frame];
Brian Webster
+1  A: 

If you have the split view, or get it by setting up an outlet, you could use convertRect:fromView:. Or to get window-relative coordinates, you could use convertRectToBase:. Bear in mind that the table's frame would be relative to its superview, so you'd use something like [[bTableView superView] convertRectToBase: bTableViewFrame].

JWWalker
+1 the `convertRect:...` methods are the proper way to do this
Dave DeLong