Xcode & Swift - Window without title bar but with

2019-01-29 19:42发布

问题:

I am currently using Swift in Xcode 6, Beta 5. I am trying to remove the title bar, or any visible difference between the title bar and the actual content. If I enable "Unified title and toolbar" in the Attributes Inspector on a Window, nothing visibly happens. I have already left the title out.
When no title is entered, the title bar will still be distinguishable because of the border line and background difference with the rest of the window, separating it from the actual content.


An excellent example would be the current Yosemite, OS X 10.10, Notes app. No title bar is visible or distinguishable, just the Close, Minimise and Resize buttons as seen here.

I have searched and visited other posts, but to no to little avail.
Those mentioned hiding the title bar altogether, but I wouldn't know how to manually re-add the Close, Minimise and Resize buttons properly, meaning they would look correct, no actual, sneaky image replacements and connections with the menu bar Close, Minimise and Resize functions.

回答1:

The new window style mask NSFullSizeContentViewWindowMask added in OS X 10.10 will do the trick.

self.window.titleVisibility = NSWindowTitleVisibility.Hidden;
self.window.titlebarAppearsTransparent = YES;
self.window.styleMask |= NSFullSizeContentViewWindowMask;

Release Notes



回答2:

For 10.10+, you can use these:

window.titlebarAppearsTransparent = true
window.movableByWindowBackground  = true

There was an official sample project for window appearance in Yosemite. You might wanna check it out.



回答3:

For Swift 3 :-

self.window.titleVisibility = .hidden
self.window.titlebarAppearsTransparent = true
self.window.styleMask.insert(.fullSizeContentView)


回答4:

You can use these:

override func viewDidAppear() {
    super.viewDidAppear()

    self.view.window?.titlebarAppearsTransparent = true
    self.view.window?.movableByWindowBackground = true
}


回答5:

Update Sept. 2017, taget 10.11:

override func viewDidAppear() {
    super.viewDidAppear()

    self.view.window?.titleVisibility = .hidden
    self.view.window?.titlebarAppearsTransparent = true
    self.view.window?.styleMask.insert(.fullSizeContentView)
}


回答6:

I don't have enough reputation to comment on Ranfei Songs answer, but running on OSX 10.12 the syntax for the titleVisibility is slightly different, instead of this:

self.window.titleVisibility = NSWindowTitleVisibility.Hidden;

you'll need to use NSWindowTitleHidden instead, so updating Ranfei's code would result in you need to specify this like this:

self.window.titleVisibility = NSWindowTitleHidden;
self.window.titlebarAppearsTransparent = YES;
self.window.styleMask |= NSFullSizeContentViewWindowMask;