In IB this can be done easily by checking the 'Resize' checkbox on or off. My problem is I want my main NSWindow to not be resizable, until a button is clicked, and then i want it to be resizable.
I've scoured the Internet but can't find anything? Can a window not be made to be resizable or not programmatically?
Thanks in advance everyone!
Press-and-hold Alt, then middle-click near the corner that you want to resize. The mouse pointer changes to indicate that you can resize from the corner. To resize the window, drag from the corner on which you middle-clicked. To resize a window horizontally point to one of the vertical edges of the window.
Move or resize a window using only the keyboard. Press Alt + F7 to move a window or Alt + F8 to resize. Use the arrow keys to move or resize, then press Enter to finish, or press Esc to return to the original position and size. Maximize a window by dragging it to the top of the screen.
If you press CTRL+SHIF+R on any window a dialog appears and you can enter the sizes that you want. The current size of the window is displayed too.
Since 10.6, you can change the style mask of a window using -[NSWindow setStyleMask:]
. So, you'd do something like this:
In Objective-C
To make it resizable:
window.styleMask |= NSWindowStyleMaskResizable;
To make it non-resizable:
window.styleMask &= ~NSWindowStyleMaskResizable;
In Swift
To make it resizable:
mainWindow.styleMask = mainWindow.styleMask | NSWindowStyleMaskResizable
To make it non-resizable:
mainWindow.styleMask = mainWindow.styleMask & ~NSWindowStyleMaskResizable
The Swift 3 solution to this issue is to use the OptionSet
class described at:
https://developer.apple.com/reference/swift/optionset
To replace the set of flags, you now do something like:
myWindow.styleMask = [ .resizable, .titled, .closable ]
To add a flag, do something like:
myWindow.styleMask.insert( [ .miniaturizable, .fullscreen ] )
To remove a flag, something like:
myWindow.styleMask.remove( [ .resizable ] )
In Swift 3,
if enabled {
window.styleMask.update(with: .resizable)
} else {
window.styleMask.remove(.resizable)
}
You can't change the style mask of a window after creating it, but you can set the window's minimum and maximum frame size to the same size. Do that after you and your resizable window awake from nib, and then change the maximum and optionally the minimum size back when the user clicks the button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With