Window resizing

I have a slight issue trying to get the main window to resize properly with a XCode project.

I have a popup button which is inside a “on action” handler and when its title equals whatever I set it to I want the window to increase in height, but some strange reason it does resize but the top of the window resizes, instead of resizing from the bottom. Am I missing something ?

Thanks in advance.

The origin of a Cocoa window is (0, 0) bottom-left. That’s why.

Cocoa drawing is Quartz-based, and the window compositor is OpenGL-based. OpenGL uses (0, 0) bottom-left. For performance and simplicity, the origin of a window in Cocoa mimics this.

All in the documentation.

Thers no need for this

That’s why.

What do you mean by this, tellboy? Resizing window’s is VERY common, especially for custom windows and panels. I’ve used resizing for a variety of tasks, most often for creating custom preference windows, for going fullscreen, or for toggling state between two distinct panes of a utility window. There are certainly times when leaving resizing to the standard window controls is appropriate, but there is also a reason why NSWindow has a half-dozen methods that enable you to programatically change the size and bounds of a window. When you say something so definitively, you should back it up with some definition of why you believe it, or you might come across wrong.

As mikey-san states, the bounds of a window start bottom-left. You must get the bounds, add or subtract the desired change in height from the origin of the bounds, add or subtract from the height, then reset the bounds. When doing this mathematically, you might also have to account for the height of the title bar (usually 22px) in your calculations. It is most often best to do this using the nswindow method “setFrame:display:animate:” and set the animate flag to true/YES so the window dynamically and fluidly resizes to the new frame.


Tell that to Apple, who does it all the time.

For clarification’s sake, NSWindow’s frame starts at (0, 0) Cartesian (bottom-left), not bounds. Worth noting the difference.

Here is my custom Cocoa method for doing this, in case anyone wants to know the steps involved:

[code]+ (NSRect)adjustedWindowFrameFromFrame:(NSRect)windowFrame newHeight:(float)h
const float edgeBottom = windowFrame.origin.y; // before we change positioning, the bottom of the window is this far from the bottom of the screen
const float oldHeight = windowFrame.size.height; // before we change height, the window is this tall
const float oldFrameTop = edgeBottom + oldHeight; // before we change anything, the top of the window is this far from the bottom of the screen

const float fDiff = oldFrameTop - h; // the difference (the new offset for the frame origin)

windowFrame.origin.y = fDiff; // set new window frame origin
windowFrame.size.height = h; // set new window height

return windowFrame; // return new window frame

This works with both titled windows and toolbar-enabled windows, assuming windowFrame comes from -[NSWindow frame]. The newHeight parameter is the new total height of the window.

You might find my problem and bodged solution in this topic to be helpful:

Hey there

I posted a reply to your ‘bodged solution’ as you called it, as I was having a few difficulties in implementing it reliably.

If you could point me in the right direction that would be cool,

Thanks very much

i to tried castle’s solution to the problem, i’ve got it to expand and contract from the top (as ryan) however, the items in the window will not stay at the top. any help would be beneficial.

hopefully by bumping this thread to the top.


The items stay at the bottom, but that’s not a problem. Just use the bottom of the window as your fixed point, placing all the window content at the bottom with empty space at the top. When the window is resized, that space gets removed - so the resize removes the empty space at the top, rather than any empty space at the bottom.

Frederickk: I found that if you load up the inspector for each object on the window, then go to the size part, you should see what looks like a layout of the screen, with straight lines going out from all four sides of the window. If you set the bottom-inner one to infinite by clicking it once, you will find that the object stays where it is even when the window expands or contracts.

I hope that will help.


ryan. that worked perfect. thanks for the tip!