Henri,

Does setting your main window aspect ratio properties with 
"setContentAspectRatio" not work.  In my own code, I have something like:

[myWindow setContentAspectRatio: myImageSize]  , 

where myImageSize is a variable of NSSize type (a pair of floats in a struct 
that give the width and height of your image), and has been anchored to an 
image view within the application's main window (myWindow).

Hope this helps, or at least points you in the right direction.

cheers
vinai

--- On Wed, 11/18/09, Henri Häkkinen <hen...@henuxsoft.com> wrote:

> From: Henri Häkkinen <hen...@henuxsoft.com>
> Subject: Keeping NSView square
> To: cocoa-dev@lists.apple.com
> Date: Wednesday, November 18, 2009, 6:38 AM
> Hello.
> 
> I'm making a custom NSView derived class and I need the
> view to have a fixed width/height ratio at all times,
> specifically I would like the view to stay square. I am
> trying to override the setFrame: method like this:
> 
> - (void)setFrame:(NSRect)frameRect {
>     // Keeps the frame rectangle square.
> 
>     CGFloat size = frameRect.size.width >
> frameRect.size.height
>         ?
> frameRect.size.height : frameRect.size.width;
> 
>     NSRect newFrameRect =
> NSMakeRect(frameRect.origin.x, frameRect.origin.y, size,
> size);
> 
>     [super setFrame:newFrameRect];
> }
> 
> And then in the drawRect: method I just fill the view with
> the current color and draw an image on it.
> 
> - (void)drawRect:(NSRect)dirtyRect {
>     NSRectFill([self bounds]);
> 
>     NSImage *image = [NSImage
> imageNamed:@"foo.pdf"];
>     [image drawInRect:NSMakeRect(0.0, 6.0,
> 1.0, 1.0)
>    
>      fromRect:NSZeroRect
>        
> operation:NSCompositeSourceOver
>    
>      fraction:1.0];   
> 
> }
> 
> This however seems not to work. The view keeps the fixed
> dimension ratio, but resizing the window does not work
> properly. When I make the window smaller, the custom view
> gets smaller, but when I resize the window to get bigger,
> the custom view does not get bigger properly. I have enabled
> Autoresizing in the Interface Builder for the custom view.
> 
> What would be the proper way of handling this? How do I
> make the custom view to stay square?
> 
> Thanks.
> 
> Regards,
> Henri Häkkinen





_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to