• 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
Re: How to keep a subview centered - autoresizing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to keep a subview centered - autoresizing


  • Subject: Re: How to keep a subview centered - autoresizing
  • From: email@hidden
  • Date: Fri, 8 Feb 2002 12:49:40 -0800

I have a view which is initially centered in its superview (which is an
NSWindow's contentView) with its autoresizingMask set to
NSViewMinXMargin | NSViewMinXMargin (i.e. springs to the left and to the
right). When the user drags the window wider, the initially-centered
view starts drifting to the left (apparently if the resize amount is
odd, then the extra pixel is always added to the right margin). What's
the Right Thing to Do if I want the view to remain centered at all
times? I suppose I can call -[NSWindow setResizeIncrements:] with even
increments. Any better ideas?

Springs and struts, as currently implemented, are really no good for anything but keeping either one or both sides of a view "stuck" to the nearest edge. Any sort of centering behavior, division of gained/lost area between multiple views, etc. has to be done by hand.
The way I typically do this is to have an ivar in which I put the frame of the views that need to be controlled, at nib load time. Whenever my window or superview (whichever is more convenient) resizes, I use the original frame and the current size of my window/superview, and calculate a new frame for my view, which I then set.
In the app I'm about to release, the resize behavior got so complex that I wrote a little utility method that gets passed the original frame, the size of the window, and a code representing how I want the view to behave. The utility method figures out the correct new frame and sets it. This let me implement custom resizing behavior on dozens of views in my window with very little effort.
Maybe someday Apple will replace springs and struts with a less broken model. But they were talking about how springs and struts was broken back when I was working at NeXT, so... you may not want to hold your breath. :->

Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: How to keep a subview centered - autoresizing
      • From: "Erik M. Buck" <email@hidden>
  • Prev by Date: Re: Final (cleanup) drawing event?
  • Next by Date: Re: Performance and NSTimee
  • Previous by thread: How to keep a subview centered - autoresizing
  • Next by thread: Re: How to keep a subview centered - autoresizing
  • Index(es):
    • Date
    • Thread