10

So I am presenting an NSViewController as a sheet of a window that has resize disabled.

The view controller that is presented as a sheet can still be resized.

How do I disable resizing of a NSViewController?

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • If you un-tick the "resize" checkbox in the interface editor you won't be able to resize the window. How do you resize the view? – Arc676 Nov 04 '15 at 15:14
  • Yes the window I can't resize but if I presentViewControllerAsSheet then I can resize that view which is what I want to avoid. –  Nov 04 '15 at 15:28

3 Answers3

29

Swift 4:

override func viewDidAppear() {
   // any additional code
   view.window!.styleMask.remove(.resizable)
}

By the way you can do this without writing code, here is how:

  • Drag a Window Controller element to the Storyboard from the Object Library.
  • Connect the Window Controller to the specific View Controller which you want to disable resize.
  • On the Window Controller's Attributes uncheck Resize option.

disable resize window xcode

balazs630
  • 3,421
  • 29
  • 46
  • 2
    This doesn't appear to work for views presented as `sheets`, as indicated in the text of the question. – pkamb Nov 01 '19 at 01:30
  • 1
    This does NOT work for a view presented by `presentAsSheet()`. – lishrimp Aug 28 '20 at 05:36
  • It works fine for me on a controller presented as a sheet. Just make sure to put the code in `viewDidAppear`, not `viewDidLoad`. `view.window` is `nil` in `viewDidLoad`. – McKinley May 30 '22 at 09:37
11

After some more trying I found out this did the trick in viewDidLoad:

self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height); 
1

If you add these methods, the issue will be fixed.

- (void)updateViewConstraints NS_AVAILABLE_MAC(10_10) {

    [super updateViewConstraints];

}

- (void)viewWillLayout NS_AVAILABLE_MAC(10_10) {

    self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height);

}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Thippi
  • 99
  • 9