Search code examples
swiftmacoscocoaappkitnsviewcontroller

NSPopover animate contentViewController change and change popover size


I am making a NSPopover, and have made it so I can transition between view controllers with a sliding animation using a parent view controller with my view controllers as children. It works well, except before I added this, the popover automatically resized to my view's size, but now, the popover is stuck at a fixed size.

The code for creating the popover:

    self.homeVC = PopoverViewController(nibName: "PopoverViewController", bundle: nil)
    self.loginVC = SignInViewController(nibName: "SignInViewController", bundle: nil)

    self.containerView.view.wantsLayer = true

    self.containerView.view.frame = self.homeVC!.view.bounds

    self.containerView.addChildViewController(self.homeVC!)

    self.containerView.view.addSubview(self.homeVC!.view)

    popover.contentViewController = self.containerView

The code for transitioning the view controllers:

    self.loginVC!.view.frame = self.homeVC!.view.bounds
    self.containerView.addChildViewController(self.loginVC!)

    let transition: NSViewControllerTransitionOptions = .SlideLeft

    self.containerView.transitionFromViewController(self.homeVC!, toViewController: self.loginVC!, options: transition, completionHandler: { finished in
        self.homeVC!.view.removeFromSuperview()
        self.homeVC!.removeFromParentViewController()
        self.containerView.view.bounds = self.loginVC!.view.bounds
    })

Is there anyway that I can make the popover automatically resize to the size it is supposed to be after transitioning?

Thanks in advance.


Solution

  • I was able to fix it by instead of explicitly setting the frame, setting constraints to make the container view match the child's size.