2016-03-14 71 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

相關問題