Reputation: 113
I am working on an app that I don't want the user to be able to resize but be able to switch between 2 sizes like the MacOS Calc app.
I managed to make it un resizable using this code window.styleMask.remove(.resizable)
found here Non-resizable window swift
The problem is the following, the code disables the resize button in the title bar.
Is there an option to mimic the behavior of the Calc app and if yes how?
Upvotes: 3
Views: 707
Reputation: 90551
Calculator.app seems to leave its window as resizable, as evidenced by the fact that it shows the resizing cursors at its edges. However, it is presumably restricting what size the window can be (depending on the calculator mode). An app can control that in a number of ways.
Calculator seems to implement windowWillResize(_:to:)
in its window delegate to always return a fixed size depending on the mode, ignoring the requested size. The evidence for this is that the resize cursors show that resizing is possible (double-headed arrows). With this mechanism the system can't determine in advance whether resizing will work because the delegate could give a different answer every time it's asked.
Another technique is to use autolayout constraints that dictate a window size at a priority higher than NSLayoutPriorityDragThatCanResizeWindow
(510).
Finally, you can set the contentMinSize
and contentMaxSize
properties of the window to the same size. (You could use minSize
and maxSize
, but the content...Size
ones are easier to work with.)
To switch sizes, you can either change the zoom button's action selector to a method of your own, override zoom(_:)
method, or implement windowWillUseStandardFrame(_:defaultFrame:)
in the delegate. The last is probably best. It should return the frame appropriate for the non-current mode.
Upvotes: 5