将NSWindow contentViewController替换为具有相同窗口大小

时间:2016-03-14 21:53:54

标签: cocoa nswindow

按钮点击后,我需要用新的ViewController替换NSWindow.contentViewController。为此,我写了以下代码:

 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处于全屏模式时,GameViewController设置为其默认大小,并且在发生调整大小事件之前不会改变大小。

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

1 个答案:

答案 0 :(得分: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)
                        }

希望这会对你有所帮助。快乐的Codding

相关问题