Luke
Luke

Reputation: 5094

Textured NSWindow with setBackgroundColor has a tinged grey background

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: 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: My about window :(

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

Is this a bug? Or is it supposed to look really grey? How can I make this window "true white"?

Upvotes: 1

Views: 1732

Answers (2)

Clifton Labrum
Clifton Labrum

Reputation: 14158

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.

Upvotes: 1

Luke
Luke

Reputation: 5094

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:

Yay

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.

Upvotes: 0

Related Questions