How do I get the inner / client size of an NSView subclass?

I am doing a manual layout for my Cocoa application and at some point I need to figure out what is the internal size of the NSView subclass. (For example, what is the height available for my child view inside the NSBox?)

One of the reasons is that I am using a coordinate system with origin in the upper left corner and need to perform coordinate transformations.

I couldn't find a way to get this size so far and would be glad if someone can give me a hint.

Another very interesting property that I would like to know is the minimum view size.

+1


a source to share


4 answers


-bounds

is the one you are looking for in most views. However, NSBox is a special case, as you want to see the bounds of the view of the contents of the box, not the bounds of the window itself (window with title, edges, etc.). In addition, the rect constraint is always the actual size of the box, while the frame rectangle can be changed relative to the bounds of applying transforms to the content of the view (for example, squashing a 200x200 image into a 200x100 frame).



So, for most views you just use [parentView bounds]

, and for NSBox you will use [[theBox contentView] bounds]

and you will use [[theBox contentView] addSubview: myView]

, not [parentView addSubview: myView]

to add your content.

+5


a source


Unfortunately, there is no standard way to do this for all NSView subclasses. In your specific example, the position and size of the child view in NSBox

can be calculated like this:

NSRect availableRect = [someNSBox bounds];
NSSize boxMargins = [someBox contentViewMargins];
availableRect = NSInsetRect(availableRect, boxMargins.width, boxMargins.height);

      

If you use this a lot, you can create a category NSBox

like this:

// MyNSBoxCategories.h
@interface NSBox (MyCategories)
- (NSRect)contentFrame;
@end

      




// MyNSBoxCategories.m
@implementation NSBox (MyCategories)
- (NSRect)contentFrame
{
    NSRect frameRect = [self bounds];
    NSSize margins = [self contentViewMargins];
    return NSInsetRect(frameRect, margins.width, margins.height);
}
@end

      

And you will use it like this:

#import "MyNSBoxCategories.h"

//...

NSRect frameRect = [someNSBox contentFrame];
[myContentView setFrame:frameRect];
[someNSBox addSubview:myContentView];

      

+5


a source


The bounds

NSView property returns an NSRect with a start (usually (0,0)) and the size of the NSView. See this Apple developer documentation page .

0


a source


I'm not sure (I've never had to go too deep into this stuff), but aren't these the boundaries of NSView?

http://www.cocoadev.com/index.pl?DifferenceBetweenFrameAndBounds

0


a source







All Articles