2

In Xcode, if you try to create a new file, it will ask you where to save it. There is this button that you can press to expand the sheet presented:

enter image description here

I am trying to create something similar to that button, which will enlarge an NSViewController presented with a Sheet segue. Note that I am not trying to create my own version of NSSavePanel. I am just trying to use this as an example to illustrate the behaviour of the button that I want to create.

I know that I can set the preferredContentSize to something bigger to enlarge the sheet:

preferredContentSize = CGSize(width: 300, height: 300)

But that changes the size immediately, unlike the button in the save file dialog, which shows an animation of the sheet enlarging.

I tried putting it in an animation block:

NSAnimationContext.runAnimationGroup { (context) in
    context.duration = 1
    preferredContentSize = CGSize(width: 300, height: 300)
}

But the same thing happens.

I'm pretty sure there should be a method in NSViewController or something like that I am failing to find...

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I don't see how presenting a window sheet has anything to do with NSOpenPanel or NSSavePanel. – El Tomato Aug 02 '19 at 13:59
  • @ElTomato My question is not directly related to `NSSavePanel`. I am just trying to recreate the behaviour of a button in `NSSavePanel`, namely, enlarging the sheet with an animation. – Sweeper Aug 02 '19 at 14:00
  • There is a method for NSWindow: `self.view.window?.setFrame(NSRect(x: 0, y: 0, width: 100, height: 100), display: true, animate: true)` – Ckacmaster Aug 03 '19 at 00:07
  • @Ckacmaster That's it! It worked! I never would have thought it would be in `NSWindow`, because I thought `view.window` refers to the actual window (with the three widgets on the top left and stuff), instead of the sheet. – Sweeper Aug 03 '19 at 01:03

1 Answers1

4

NSWindow has a method to do exactly this:

self.view.window?.setFrame(NSRect(x: 0, y: 0, width: 100, height: 100), display: true, animate: true)
Ckacmaster
  • 366
  • 1
  • 10
  • While applying this solution my window is sliding from top, but i want to reduce the size from bottom.. can u please tell me what can be the issue? – hmali Jul 23 '20 at 07:15
  • @hmali you may need to adjust your view constraints. Are you using NSSavePanel or a custom NSViewController for presentation? – Ckacmaster Sep 29 '20 at 00:19