• 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: How to center a custom NSView in a NSScrollView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to center a custom NSView in a NSScrollView


  • Subject: Re: How to center a custom NSView in a NSScrollView
  • From: Matt Neuburg <email@hidden>
  • Date: Thu, 26 Feb 2009 11:10:26 -0800
  • Thread-topic: How to center a custom NSView in a NSScrollView

>If the visible area (NSScrollView) is GREATER than the fixed size of
>the custom NSView, the custom NSView should be positioned centered in
>the NSScrollView, otherwise, the default behaviour of the scroll view
>should apply. I just want to avoid that the custom NSView stays at the
>bottom left (or top left) corner of the NSScrollView, when there's
>enough space to center it horizontally and vertically.

You don't need to subclass NSClipView or anything else. You just need to
draw your custom NSView in such a way that it does what you just said.

So, I have a custom NSView in a clip view in a scroll view. The job of the
NSView is to draw an image. I want that image centered as the scroll view
changes size. So (cv is the clip view, sv is the scroll view, joco is the
image):

- (void)drawRect:(NSRect)rect {
    [[NSColor whiteColor] set];
    NSRectFill(rect);
    NSSize mySize = [self frame].size;
    NSSize imageSize = [self->joco size];
    NSPoint orig;
    orig.x = (mySize.width - imageSize.width) / 2.0;
    orig.y = (mySize.height - imageSize.height) / 2.0;
    [joco dissolveToPoint: orig fraction: 1.0];
    curTop = [cv bounds].origin.y + [cv bounds].size.height;
}

- (void) sizeChanged:(NSNotification*)aNotif {
    NSSize scrollSize = [sv contentSize];
    NSSize imageSize = [joco size];
    NSSize mySize;
    mySize.width = (scrollSize.width > imageSize.width) ? scrollSize.width :
imageSize.width;
    mySize.height = (scrollSize.height > imageSize.height) ?
scrollSize.height : imageSize.height;
    [self setFrameSize: mySize];
    if (scrollSize.height < imageSize.height)
        [cv setBoundsOrigin: NSMakePoint([cv bounds].origin.x, curTop -
scrollSize.height)];
}

m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: NSLayoutManager for NSTextField (was Re: Metrics for a NSTextField)
  • Next by Date: Re: One IBAction, multiple results from multiple methods
  • Previous by thread: Re: How to center a custom NSView in a NSScrollView
  • Next by thread: Help with sheets, NSArrayController & NSTableView (all working together) ...
  • Index(es):
    • Date
    • Thread