9

I'm new in Mac OSx / Cocoa development. During the creation of my first app, I bumped into a few things, and one of which is my issue on the green + button which is intended for zooming purposes.

I'd like to know if it is possible to dynamically set the behavior of the zoom button of the app window? Am I not violating any rule from Apple's guidelines?

I want to specify the behavior of the button according to a specific user. Say, the user is allowed to have a zooming button, then the button should be enabled; otherwise, leave the button disabled.

In this case, when the app is launched, I am checking if the user is allowed to have an enabled zoom button. From here I want to customize the behavior of the window pertaining to the zoom button - whether it should be enabled or disabled according to the prior checking of the user's mode.

Thanks ahead for the help!

Kimpoy
  • 1,974
  • 3
  • 21
  • 38
  • Why would zooming ever be restricted? Its actions would be based on the content's windowing size, which is *not* secret information, since the scrollbars need it too. The user could get around restriction by sizing the window themselves. The window's size is a user-side terminal attribute, not something that should be controlled by the server end. – CTMacUser Jul 31 '14 at 22:08

3 Answers3

12

You can get a reference to that button with standardWindowButton:NSWindowZoomButton, and then do whatever you can do with any NSButton.

Update (in swift):

var button = view.window?.standardWindowButton(NSWindowButton.ZoomButton)
button?.enabled = false
Q8i
  • 1,767
  • 17
  • 25
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • See Sam Soffes' answer below for the correct way to do this. – Tom Hamming Mar 22 '17 at 21:59
  • 2
    Thanks! If you want to keep your window resizable, but don't want your application to fill the whole screen on button press, this code disables the green zoom button. – balazs630 Jul 01 '17 at 12:36
7

Grabbing the button and setting enabled is not ideal. The best way (10.6+) is to use setStyleMask:. Here's how to do this:

window.styleMask = NSTitledWindowMask | NSClosableWindowMask

You can add or remove styles at will. Another way to do this without changing the style is to set minSize and maxSize to the same size. This will also disable resizing.

Sam Soffes
  • 14,831
  • 9
  • 76
  • 80
2

In Swift 3, here is the easiest way to remove the ability:

var style = window.styleMask
style.remove(.resizable)
window.styleMask = style

I commonly do this in viewDidAppear for view controllers in storyboards that have a window controller created for them automatically instead of one I can mess with.

Sam Soffes
  • 14,831
  • 9
  • 76
  • 80