【问题标题】:Replace NSWindow contentViewController with new ViewController with same Window Size用具有相同窗口大小的新 ViewController 替换 NSWindow contentViewController
【发布时间】:2016-03-14 21:53:54
【问题描述】:

我需要在单击按钮时将 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 以占用所有窗口空间?

【问题讨论】:

    标签: cocoa nswindow


    【解决方案1】:

    如果您使用情节提要:

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

    希望这会对您有所帮助。快乐编码

    【讨论】:

    • 不,我没有使用 Storyboard。我以编程方式添加所有组件。
    猜你喜欢
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2013-03-24
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多