leinadsey
leinadsey

Reputation: 161

How do you reference the view's window in Swift 3.x using Storyboards/Cocoa

with all the changes in Xcode and Swift, I can't figure out how to address the view's window in a stotyboard-driven project in the way I'm doing it in projects using XIBs. New to macOS programming so apologies in advance if this is basic stuff:

When using a storyboard, how do I change the view's window state using for instance:

window.titleVisibility = .hidden
window.setContentSize(size)

In an xib-driven project, I'm using

@IBOutlet weak var window: NSWindow!

but this doesn't seem to work the same way with storyboards. How do I do the same tihing using storyboards? Any help appreciated!

Upvotes: 1

Views: 2212

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236360

You can get a reference of your view window accessing its window property. Note that it can not be done inside view did load but you can create a window property for your view controller using a lazy initializer:

lazy var window: NSWindow! = self.view.window

And you can do your window customization inside the method viewWillAppear:

import Cocoa


class ViewController: NSViewController {

    lazy var window: NSWindow! = self.view.window

    override func viewWillAppear() {
        super.viewWillAppear()
        window.titleVisibility = .hidden
    }
}

Upvotes: 5

leinadsey
leinadsey

Reputation: 161

This seems to work:

if let window = NSApplication.shared().mainWindow {
    window.titleVisibility = .hidden
}

Is this an ok way to do it?

Upvotes: 1

Related Questions