2016-03-14 30 views
1

我需要在按钮单击时用新的ViewController替换NSWindow.contentViewController。要做到这一点,我写了下面的代码:将NSWindow contentViewController替换为具有相同窗口大小的新ViewController

let g = GameViewController() 

if self.window!.styleMask & NSFullScreenWindowMask != NSFullScreenWindowMask { 
    let ws = NSScreen.mainScreen()?.frame.size 
    let width : CGFloat = g.view.frame.width 
    let height : CGFloat = g.view.frame.height 

    let frame = NSRect(origin: NSMakePoint(((ws?.width)!-width)/2, ((ws?.height)!-height)/2), size: CGSize(width:width, height:height)) 

    self.window?.setFrame(frame, display: true, animate: true) 
} 

self.window?.contentViewController = g 

当NSWindow不在全屏模式下,此代码的工作好。而当NSWindow处于FullScreen模式时,GameViewController会以其默认大小进行设置,而不会改变大小,直到发生调整大小事件。

如何实例化NSViewController以占用所有窗口空间?

回答

0

如果您使用的故事板:

let storyboard = NSStoryboard(name: "Main", bundle: nil) 
         if let homeViewController = (storyboard.instantiateControllerWithIdentifier("HomeViewController") as? NSViewController){ 
          let windowController = NSWindowController(window: Constants.appDelegate!.window) 
          windowController.contentViewController = homeViewController 
          windowController.showWindow(Constants.appDelegate!.window) 
         } 

希望这会帮助你。 Happy Codding

+0

不,我没有使用Storyboard。我以编程方式添加所有组件。 – Alessandro

相关问题