Re: Centering an NSImageView in NSScrollView (again)
Re: Centering an NSImageView in NSScrollView (again)
- Subject: Re: Centering an NSImageView in NSScrollView (again)
- From: John Pannell <email@hidden>
- Date: Mon, 14 Oct 2002 10:15:53 -0600
Hi Jan-
I have a similar view in my application, and pursue a similar strategy
to center the image in the NSScrollView. I believe the issue with your
code is that you size the view to the image size, rather that the
NSScrollView's visible size in your setImage method. Here's my strategy:
1. In IB, make the NSImageView autoresize with the NSScrollView it is
in.
2. When initializing the interface (in awakeFromNib, for example) set
the NSImageView to be a centering view, like so:
[bigImageView setImageAlignment:NSImageAlignCenter];
3. In the method that actually assigns the image to the view, assign
the width and height of the view to whichever is larger: the image size
or the NSScrollView visible size, like so:
imageWidth = [[myImage] size].width;
imageHeight = [[myImage] size].height;
viewWidth = [[bigImageView superview] visibleRect].size.width;
viewHeight = [[bigImageView superview] visibleRect].size.height;
if(imageWidth >= viewWidth){
setWidth = imageWidth;
} else {
setWidth = viewWidth;
}
if (imageHeight >= viewHeight){
setHeight = imageHeight;
} else {
setHeight = viewHeight;
}
[bigImageView setFrame:NSMakeRect(0, 0, setWidth, setHeight)];
4. Then, go ahead and assign the image to the view:
[bigImageView setImage:myImage];
The result of the above setup is a view that automatically recenters the
image in the view as the user resizes the window or view (setting
NSImageAlignCenter and making sure the view autoresizes to the
NSScrollView takes care of this for you; you do not need the
windowDidResize notification that you are using below). Additionally,
if the image is taller or wider than the display area of the scroll
view, then the NSImageView is sized correctly and the scroll bars become
active. If the window is dragged large enough to encompass the
NSImageView, than the scrolls adjust and eventually disappear, and the
image remains centered in the scroll view.
Hope this helps!
John P.
On Friday, October 11, 2002, at 09:47 PM, Jan Van Tol wrote:
I'm using the following code in an attempt to center my NSImageView in
an NSScrollView:
In my setImage: method:
[imageView setImage:image];
[scrollView setDocumentView:comicView];
[imageView setNeedsDisplay:YES]; // Is this necessary?
[imageView setFrameSize:[image size]];
[imageView
setImageAlignment:(NSImageAlignment)NSImageAlignCenter]; // Probably not
necessary
Then:
- (void)windowDidResize:(NSNotification *)aNotification {
NSLog(@"windowDidResize"); // yes, this does run.
NSSize scrollViewSize = [scrollView frame].size;
NSSize frameSize = [imageView frame].size;
NSPoint frameOrigin;
frameOrigin.x = frameOrigin.y = 0.0;
if ( scrollViewSize.width >= frameSize.width ) {
frameOrigin.x=((long) (scrollViewSize.width-
frameSize.width))/2;
}
if ( scrollViewSize.height >= frameSize.height ) {
frameOrigin.y=((long) (scrollViewSize.height-
frameSize.height))/2;
}
[imageView setFrameOrigin:frameOrigin];
}
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?
Thanks in advance,
-Jan Van Tol
_______________________________________________
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.
_______________________________________________
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.