4

I have a NSView, there is a NSImageView on the NSView

in the source codes of the NSView

I wrote:

NSImage *image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease]; NSImageView *newImageView = nil; newImageView = [[NSImageView alloc] initWithFrame:[self bounds]]; [newImageView setImage:image]; [newImageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; 

Is there a way to read the width and height of the NSView and NSImageView?

2 Answers 2

7

The size of any view (NSView, NSImageView, etc.) is view.frame.size or view.bounds.size. In both cases it is an identical NSSize struct. You can write code like this:

NSSize size = newImageView.frame.size; NSLog(@"size: %@", NSStringFromSize(size)); NSLog(@"size: %f x %f", size.width, size.height); 

To change it, you need to update the view frame property:

NSRect frame = view.frame; frame.size = NSMakeSize(<#new width#>, <#new height#>); view.frame = frame; 
Sign up to request clarification or add additional context in comments.

Comments

2

Try the - (NSRect)bounds method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.