• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Centering an Image in an NSScrollView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Centering an Image in an NSScrollView


  • Subject: Re: Centering an Image in an NSScrollView
  • From: Charles Jolley <email@hidden>
  • Date: Mon, 1 Jul 2002 09:19:16 -0500

The best place I found to do this is in -resizeWithOldSuperviewSize: for my document view (your Image). The logic is pretty simple: check the bounds of the superview. For each direction (x,y), if the bounds size is larger than my image view, I expand my image size to the bounds size. If it is smaller, than I will reduce my size to the minimum size I can handle. Then I would adjust the document view bounds so that my image is always place in the center.

This approach assumes that your document view is not going to change the size of its contents when you adjust its frame.
On Monday, July 1, 2002, at 02:54 AM, jean-michel daix wrote:

Hi,

As I think many people, I'm trying to centering an Image in a document based
application using an NSScrollView. I mean when I enlarge my window, I want
my image to stay centered in the window. In my custom ImageView I use this
code.

- (BOOL)isFlipped
{
return YES;
}

- (void)drawRect:(NSRect)rect {
[self hideOrShowScrollers]; //Test if the image is bigger than the view
[self centerFrame];
[super drawRect:rect];
}

- (void)centerFrame
{
NSSize scrollViewSize = [[[self superview] superview] frame].size;
NSSize frameSize = [self 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;

[[self superview] setFrameOrigin:frameOrigin];
}


Well, it works. Except that sometimes the background is not refreshed (When
using the zoom buttton for exemple), so I think it's not the right method to
do that. May be someone can tell me a better way or how to refresh my
background. Thanks.


-----------------------------------
Jean-Michel DAIX / <email@hidden>
Sorry, I9m French !

visit <http://www.adforum.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.
_______________________________________________
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.

References: 
 >Centering an Image in an NSScrollView (From: jean-michel daix <email@hidden>)

  • Prev by Date: Re: Gif to TIFF and transparency
  • Next by Date: Re: NSCopyBits and expose events
  • Previous by thread: Re: Centering an Image in an NSScrollView
  • Next by thread: Re: Centering an Image in an NSScrollView
  • Index(es):
    • Date
    • Thread