Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 将NSWindow contentViewController替换为具有相同窗口大小的新ViewController_Cocoa_Nswindow - Fatal编程技术网

Cocoa 将NSWindow contentViewController替换为具有相同窗口大小的新ViewController

Cocoa 将NSWindow contentViewController替换为具有相同窗口大小的新ViewController,cocoa,nswindow,Cocoa,Nswindow,单击按钮时,我需要将NSWindow.contentViewController替换为新的ViewController。为此,我编写了以下代码: let g = GameViewController() if self.window!.styleMask & NSFullScreenWindowMask != NSFullScreenWindowMask { let ws = NSScreen.mainScreen()?.frame.size let width

单击按钮时,我需要将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处于全屏模式时,GameViewController将设置为其默认大小,并且在发生调整大小事件之前不会更改大小


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

如果您使用的是情节提要:

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

希望这对你有帮助。快乐编码

不,我不使用故事板。我以编程方式添加所有组件。