问题
Basically I want to make a simple about window that has a unified title bar and window (i.e textured) and a white background, like Xcode's about window:
![](https://www.eimg.top/images/2020/03/25/b3dbff094e1e67ddea5a14c1f2b4c992.png)
So I have a textured window in IB and I have it connected to my app delegate via bindings. I then add this line of code to the app delegate:
[about setBackgroundColor:[NSColor whiteColor]];
Whilst for other colours like red and blue the window seems to change colour fine, but whenever I use [NSColor whiteColor] the window looks nowhere near as dazzlingly bright as Xcode's window:
![](https://www.eimg.top/images/2020/03/25/aaf4ad8d8ee07cbd9293e39780c80e71.png)
Interestingly enough, when the window is inactive I end up getting Xcode's white colour:
![](https://www.eimg.top/images/2020/03/25/3a4d0ee4696655fbf2ff9cb12157cb7a.png)
Is this a bug? Or is it supposed to look really grey? How can I make this window "true white"?
回答1:
You don't need a hacky solution do to this (at least not anymore).
Create a view controller and then open it as a modal
. In the view controller's subclass code, add this:
class YourVC: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Set the background color to white
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.white
}
override func viewDidAppear() {
//Customize the window's title bar
let window = self.view.window
window?.styleMask.insert(NSWindowStyleMask.unifiedTitleAndToolbar)
window?.styleMask.insert(NSWindowStyleMask.fullSizeContentView)
window?.styleMask.insert(NSWindowStyleMask.titled)
window?.toolbar?.isVisible = false
window?.titleVisibility = .hidden
window?.titlebarAppearsTransparent = true
}
}
This is using Swift 3 in Xcode 8.
回答2:
I found a tutorial that uses a pretty darn ghetto hack to swap the frame view's drawRect: method: http://parmanoir.com/Custom_NSThemeFrame
It doesn't even require you to subclass NSWindow, but I did it anyway in case I wanted to use it on another window or something.
The final result:
![](https://www.eimg.top/images/2020/03/25/4734240bba6e4c4294e18de703255de1.png)
Edit: I submitted a feedback assistant report a while back, and as of either Yosemite DP 7 or Xcode 6.1 this issue seems to be fixed. Setting the background colour of a textured window now produces a non-tinged colour.
来源:https://stackoverflow.com/questions/25006794/textured-nswindow-with-setbackgroundcolor-has-a-tinged-grey-background