Search code examples
swiftmacosnsviewcontroller

How to animate the size change of an NSViewController presented as a sheet?


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...


Solution

  • 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)