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 ?
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.
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.
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.