Re: Centering an NSImageView in NSScrollView (again)
Re: Centering an NSImageView in NSScrollView (again)
- Subject: Re: Centering an NSImageView in NSScrollView (again)
- From: Brock Brandenberg <email@hidden>
- Date: Sat, 12 Oct 2002 10:24:48 -0500
Hi Jan.
>
I'm using the following code in an attempt to center my NSImageView in
>
an NSScrollView:
>
>
code...
>
>
The problem being, simply, the imageView doesn't center. It stays
>
stubbornly anchored to the lower-left corner (yes, even after resizing
>
the window.
>
Can anyone tell me what's wrong here?
In a nutshell, there are several messages get sent between objects behind
the scenes in the scroll view hierarchy that are overriding your new origin.
It's the job of the NSClipView to do the view positioning, and as a result,
it has a method called constrainScrollPoint: that will reposition the view
if coordinates are not considered correct for its default positioning
method. There are several brute-force methods previously mentioned on the
list to force a scroll view position from within drawRect: methods, but they
are horribly inefficient. They cause internal messaging on every redraw so
it's a hack at best.
A proper method is to subclass NSClipView and do the proper positioning in
it. I've started a tutorial for Cocoa Dev Central to explain an efficient
way to center scroll views, but I haven't completed it yet. OmniWeb also
does this in browser windows that display graphics, so their might be a
subclass in the OmniFrameworks to look at. I haven't browsed thru them yet,
which is one of the reasons I haven't yet completed the tutorial.
To get you started, just subclass NSClipView and swap out your subclass at
runtime with the default one (in the NSScrollView hierarchy) like this:
-(void)windowControllerDidLoadNib:(NSWindowController *) aController
{
id docView = [[theScrollView documentView] retain];
id myClipView = [[SBCenteringClipView alloc] initWithFrame:[[theScrollView
contentView] frame]];
[myClipView setBackgroundColor:[NSColor windowBackgroundColor]];
[theScrollView setContentView:(NSClipView *)myClipView];
[theScrollView setDocumentView:docView];
[docView release];
...
}
Now you can override methods like constrainScrollPoint: in your subclass
with debugging statements and see when and what the clip view is overriding.
I have code for this, but I'm not yet convinced that I can't simplify it
more, so I'm not comfortable turning it loose yet :) It may cause more harm
than good. Last note... Make sure that the constrainScrollPoint: returns
integer coordinates as there can be some odd "smearing" happening when the
your view draws. It appears to be a bug in Cocoa somewhere, but I don't know
where.
Bye.
Brock Brandenberg
----- industrial design @ www.bergdesign.com ------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.