14

Is there a way to prevent a view from being resized if the view controller is presented as a sheet using the method presentViewControllerAsSheet or using a segue of style "sheet"?

Note that modal/show segues can be implanted to a window controller which can be set as non resizable from the storyboard itself. Segues of type popover are non-resizable by default.

resizable sheet

pkamb
  • 33,281
  • 23
  • 160
  • 191
ZestyZest
  • 911
  • 13
  • 27

4 Answers4

12

Have you tried setting your sheet view controller's -preferredContentSize? Failing that, what about adding width and height NSLayoutConstraints to the view controller's view on -viewDidLoad?

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • PreferredContentSize fixes this issue. – ZestyZest May 08 '15 at 08:52
  • 1
    was searching all over until I found this post, the preferedContentSize works magic... in Swift under viewDidLoad: self.preferredContentSize = NSSize(width: 500.0, height: 700.0) – DonBaron Oct 13 '15 at 22:47
  • No way to do this in Interface Builder? – jeff-h Jul 11 '16 at 07:05
  • Not that I've noticed recently. – Joshua Nozzi Jul 11 '16 at 13:53
  • 2
    Here's a slight modification that will pick up the size automatically from IB/constraints: `preferredContentSize = view.frame.size` – mkerley Jul 13 '16 at 21:19
  • 1
    I wouldn't rely on that. The timing of the layout cycles isn't guaranteed since Auto Layout, Core Animation, and possibly more things touch the view's frame at different times. You should *never* tie a layout constraint to anything's frame for this reason. Thst it happens to work now is lucky but that could change at any time. – Joshua Nozzi Jul 13 '16 at 21:39
3

This worked for me. The sheet is no longer resizable:

override func viewWillLayout() {
    preferredContentSize = view.frame.size
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
1

None of the other solutions worked for me, so I ended up using an NSWindow as the sheet controller (rather than a NSViewController on its own).

Declare an NSWindowController object in your header file:

NSWindowController *detailWindow;

To open the window in the form of a sheet, use the following code:

NSStoryboard *storyFile = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
detailWindow = [storyFile instantiateInitialController];
[self.view.window beginSheet:detailWindow.window completionHandler:nil];

To close the window:

[self.view.window endSheet:detailWindow.window];

In order to stop the window sheet from being resized, simply set the NSWindow minimum/maximum size in interface builder:

enter image description here

That's it, super easy! (Don't forget to set the window controller, as the initial controller).

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
0

the above methods did not work for me . self.view.autoresizesSubviews = NO; this line in viewcontroller should do the trick if the above methods didnt work

mjl007
  • 735
  • 2
  • 11
  • 27