Cocoa 将NSWindow contentViewController替换为具有相同窗口大小的新ViewController
单击按钮时,我需要将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
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)
}
希望这对你有帮助。快乐编码不,我不使用故事板。我以编程方式添加所有组件。