Re: Subview frames and bounds
Re: Subview frames and bounds
- Subject: Re: Subview frames and bounds
- From: Andy Lee <email@hidden>
- Date: Mon, 3 Nov 2008 08:57:08 -0500
On Nov 3, 2008, at 8:23 AM, DKJ wrote:
I'm still puzzled by frames and bounds.
Short answer: A view's bounds is specified in the view's coordinate
system. Its frame is
specified in its superview's coordinate system.
Longer answer:
I wonder if you're unclear on coordinate systems in general?
When you specify a rectangle, ask yourself where the coordinates and
dimensions are measured from, and in which direction.
Suppose you have a view in the middle of the window. Where is the
point (0, 0) (also known as the origin) in the view's coordinate
system? Where is it in its superview's coordinate system? Now where
is the point (100, 200) in the two coordinate systems? You have to be
able to answer those questions to understand what's going on.
Note that coordinate systems can be scaled, rotated, translated, and
flipped, which can affect the answers. I'm assuming the default case
where you haven't done any of that.
At the risk of confusing things, let me try a metaphor. I live in a
city with streets on a grid, and I live on 14th Street and 8th
Avenue. Can you tell me right now -- how far is 16th Street and 6th
Avenue, and in which direction? Is it north, south, east, west, or
some combination? Obviously you can't answer without additional
information. It depends on where "Zero'th" Street and "Zero'th"
Avenue are in my particular city, and which direction the city
planners happened to orient the streets and avenues, and how far apart
the streets and avenues are spaced.
Same with coordinate systems. You have to understand which coordinate
system you're using and where (0, 0) is and in which direction x and y
go in that coordinate system.
--Andy
I put this in the drawRect: of MyView:
[[NSColor redColor] set];
NSRectFill( self.frame );
[[NSColor blueColor] set];
NSFrameRect( self.bounds );
I put this in the awakeFromNib of my controller:
NSRect r = NSMakeRect( 10.0, 10.0, 200.0, 200.0 );
MyView *mv = [[MyView alloc] initWithFrame:r];
[[theWindow contentView] addSubview:mv];
The blue bounds rectangle is drawn just where I would expect: inset
10 pixels from the lower left corner of the window. But the red
frame rectangle is inset another 10 pixels from the lower left
corner of the blue bounds rectangle.
When I set the origin of r to (0.0, 0.0), the red and blue
rectangles coincide.
What's happening here?
dkj
_______________________________________________
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