Textured NSWindow with setBackgroundColor has a tinged grey background

五迷三道 提交于 2019-12-24 08:22:41

问题


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:

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:

Interestingly enough, when the window is inactive I end up getting Xcode's white colour:

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!