Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Window Resizable

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!

like image 496
Cristian Avatar asked May 06 '12 19:05

Cristian


People also ask

How do you make a window resizable?

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.

How do I resize a window size?

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.

How do I resize a window to 16 9?

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.


4 Answers

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
like image 84
Ken Thomases Avatar answered Oct 10 '22 08:10

Ken Thomases


The Swift 3 solution to this issue is to use the OptionSet class described at:

https://developer.apple.com/reference/swift/optionset

In short:

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 ] )
like image 31
uliwitness Avatar answered Oct 10 '22 09:10

uliwitness


In Swift 3,

if enabled {
  window.styleMask.update(with: .resizable)
} else {
  window.styleMask.remove(.resizable)
}
like image 4
onmyway133 Avatar answered Oct 10 '22 10:10

onmyway133


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.

like image 4
Peter Hosey Avatar answered Oct 10 '22 09:10

Peter Hosey