• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Keeping NSView square
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Keeping NSView square


  • Subject: Keeping NSView square
  • From: Henri Häkkinen <email@hidden>
  • Date: Wed, 18 Nov 2009 14:38:27 +0200

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 (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

  • Follow-Ups:
    • Re: Keeping NSView square
      • From: Andrew Farmer <email@hidden>
    • Re: Keeping NSView square
      • From: vinai <email@hidden>
  • Prev by Date: Re: Help needed in Writing network usage monitor application
  • Next by Date: Re: setToolTip: not working
  • Previous by thread: Re: Help needed in Writing network usage monitor application
  • Next by thread: Re: Keeping NSView square
  • Index(es):
    • Date
    • Thread