Re: Centering a view in an NSScrollView
Re: Centering a view in an NSScrollView
- Subject: Re: Centering a view in an NSScrollView
- From: Christopher B Hamlin <email@hidden>
- Date: Tue, 22 Jan 2002 01:34:49 -0500
On Monday, January 21, 2002, at 04:35 PM, Vince DeMarco wrote:
On Sunday, January 20, 2002, at 01:01 PM, Christopher B Hamlin wrote:
On Saturday, January 19, 2002, at 10:32 PM, Dustin Mierau wrote:
I have a view in an NSScrollView, when the contentSize of the
scrollview is larger than my view I want my view to appear centered
within the scrollview, even as the scrollview is being resized. Right
now it is tagged to the bottom left. I know I can return YES from the
isFlipped method to tag it to the top left, but like I said, I want
it centered. There does not seem to be a good way to do this, any
ideas?
OK, try this:
You need to get to the contentView. I think this is the superview of
your view, but I just made an outlet to the NSScrollview called
theScrollView. I had an ivar called savedBounds that was
initialized in awakeFromNib as
savedBounds = [[theScrollView contentView] bounds];
Or just this
size = [theScrollView contentSize]
and set the size of the contentView to the size returned above
frame = [[theScrollView contentView] frame];
frame.size = size;
[[theScrollView contentView] setFrame:frame]
I tried this, but it doesn't seem to work. I don't think I'm doing
anything to defeat
it, but can't get it to work. In logging, I see this at startup:
content frame is (1.000000,1.000000), 530.000000 wide, 405.000000 high)
content bounds is (-670.000000,0.000000), 530.000000 wide, 405.000000
high)
document frame is (-670.000000,0.000000), 1200.000000 wide, 900.000000
high)
document bounds is (0.000000,0.000000), 1200.000000 wide, 900.000000
high)
theScrollView contentSize is 530.000000 wide, 405.000000 high)
The content frame and bounds are the same size, as are the document frame
and bounds. Calling [theScrollView contentSize] gives the content
frame/bounds size.
If I maximize I get:
content frame is (1.000000,1.000000), 1223.000000 wide, 822.000000 high)
content bounds is (-670.000000,0.000000), 1223.000000 wide, 822.000000
high)
document frame is (-670.000000,0.000000), 1200.000000 wide, 900.000000
high)
document bounds is (0.000000,0.000000), 1200.000000 wide, 900.000000
high)
theScrollView contentSize is 1223.000000 wide, 822.000000 high)
where the content frame/bounds are both opened up at the same time.
Seems OK,
to me? The frame is opened up by the window, and the bounds open too to
show more
(rather than zooming in). But the contentSize also changes, so I can't
see how
your code will change anything. The documentation doesn't explain if
contentSize
is the contentView frame or bounds.
If I scroll down I get this:
content frame is (1.000000,1.000000), 1223.000000 wide, 822.000000 high)
content bounds is (-670.000000,78.000000), 1223.000000 wide, 822.000000
high)
document frame is (-670.000000,0.000000), 1200.000000 wide, 900.000000
high)
document bounds is (0.000000,0.000000), 1200.000000 wide, 900.000000
high)
theScrollView contentSize is 1223.000000 wide, 822.000000 high)
The content bounds have just been shifted down. Makes sense.
Zooming (adjusting the content bounds/origin to zoom in around
the currently viewed center of the document) then gives this:
content frame is (1.000000,1.000000), 1223.000000 wide, 822.000000 high)
content bounds is (-547.700012,160.199997), 978.400024 wide, 657.599976
high)
document frame is (-670.000000,0.000000), 1200.000000 wide, 900.000000
high)
document bounds is (0.000000,0.000000), 1200.000000 wide, 900.000000
high)
theScrollView contentSize is 1223.000000 wide, 822.000000 high)
which seems to settle contentSize (it is the content frame size, not
bounds). But if
contentSize is the frame size, then how can the above code change
anything?
Also, shouldn't I leave the content frame alone, to be handled by the
superview
or window?
If I then resize the window, the content frame and bounds are resized at
the
same rate, so my zoom stays. That is soooo cool!
Is there more documentation or an example
I'm missing that would help with understanding the interactions in the
NSScrollView system? The code I have now works OK for center-based
zooming
and also I can do what Dustin asked for, but think I'm missing something
big
in what you are suggesting. Just when I thought I had it figured
out . . .
thanks,
Chris Hamlin