Re: NSScrollView and Custom Views
Re: NSScrollView and Custom Views
- Subject: Re: NSScrollView and Custom Views
- From: James Housley <email@hidden>
- Date: Mon, 27 Jun 2005 10:22:30 -0400
On Jun 27, 2005, at 9:34 AM, Joshua Scott Emmons wrote:
Anyone have any ideas? Am I way off course?
Can't say if you're off course or not, but I've never been able to get
my custom view out of the lower-left corner (or upper-left if I've
overridden -isFlipped to return YES) when working with a scroll view.
But, truth be told, you really don't need it to be moved anywhere
else. You should be thinking of that view as the piece of paper that
you will draw all of your other stuff on. The only thing it needs to
worry about is resizing itself so that whatever it contains is
displayed. When you think of it that way, it doesn't make much sense
to have this:
+---------------+
| |
| +------+ |
| | text | |
| +------+ |
| |
+---------------+
Because if the view gets too big, it'll look like this:
+---------------+ ^
| | *
| +-----------| |
| | text is cu| |
| | off at odd| |
| | because | |
+---------------+ v
<=-------------->
The user will scroll so that the document fits:
+--------------+ ^
|+------------+| |
|| text is cut|| *
|| off at odd || |
|| because of || |
|+------------+| |
+--------------+ v
<---=----------->
So now everything looks as it should, but the scroll bars are still
there, and their thumbs are at wonky positions because of that big
margin you have in the upper left.
A better solution is to size your custom view (the paper) to be the
same size as the scroll view that contains it, and then center the
thing you want to display in the custom view. Then you can resize
your custom view as its contents grows and the scroll view will Do The
Right Thing.
Cheers,
-Joshua Emmons
Yes you can do it and it isn't too hard. The following code is
something I modified from an example I found. You need to subclass
NSScrollView and implement tile: yourself. A search should be able to
find it. The example kept the "object" centered. I wanted mine full
width and at the top, but here is my code:
- (void)tile
{
float scrollPos = [[self verticalScroller] floatValue];
NSRect newContentRect = [self frame];
NSRect documentRect = [[self documentView] frame];
float scrollerWidth = NSWidth([[self verticalScroller] frame]); // 15
pixels for normal scrollers
// assuming that both scrollers are always visible! Modify if needed :]
newContentRect = NSMakeRect(0,0,newContentRect.size.width -
scrollerWidth, newContentRect.size.height);
if ([self hasVerticalScroller])
{
[[self verticalScroller]
setFrame:
NSMakeRect(newContentRect.size.width,0,scrollerWidth,newContentRect.size
.height)];
}
// Set the horizontal size and position
newContentRect.origin.x = 1;
newContentRect.size.width -= 1;
newContentRect.origin.y = 1;
newContentRect.size.height -= 2;
if (newContentRect.size.height > documentRect.size.height)
{
newContentRect.size.height = documentRect.size.height;
}
// Calculate the free space and the scroll to point
NSPoint basePoint = NSZeroPoint;
basePoint.y = NSHeight(documentRect) - NSHeight(newContentRect) -
((NSHeight(documentRect) - NSHeight(newContentRect)) * scrollPos);
// draw the content view
[[self contentView] setFrame:newContentRect];
// set the scroll to the proper position
[[self contentView] scrollToPoint:basePoint];
[self reflectScrolledClipView:[self contentView]];
}
I see that Examples/AppKit/TextEdit/ScallingScrollView.m overloads tile.
Jim
--
/"\ ASCII Ribbon Campaign .
\ / - NO HTML/RTF in e-mail .
X - NO Word docs in e-mail .
/ \ -----------------------------------------------------------------
email@hidden http://www.FreeBSD.org The Power to Serve
email@hidden http://www.TheHousleys.net
---------------------------------------------------------------------
Q: Because it reverses the logical flow of conversation.
A: Why is putting a reply at the top of the message frowned upon?
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden