4
votes

I got an issue with NSWindow and it's content view - with appearance of title bar and rounded bottom corners to be specific. I'm observing the issue on 10.11 and on 10.10 everything looks fine. But to the point.

The title bar of the window looks like this:

correct titlte bar

and on the same time bottom looks like this:

enter image description here

No rounded corners. Window is initiated programmatically and it consists of two subviews added programmatically, by using self.contentView addSubview: method.

After some work found out, that adding

[self.contentView setWantsLayer:YES]; 

at the beginning of window init the tittle bar looks like this:

enter image description here

Flat and solid darker than normal color - not good. On the other hand bottom looks as I need it to look like:

enter image description here

I'm not allowed to show more of the windows. And here's the init with my experiments:

[self.contentView setWantsLayer:YES];

self.identifier = someid;
self.styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
self.backingType = NSBackingStoreBuffered;
[self setFrame:NSMakeRect(0, 0, windowInitWidth, windowInitHeight) display:NO];
[self setBackgroundColor:[NSColor colorForKeyPath:@"blackcolor"]];
NSButton *windowButton = [self standardWindowButton:NSWindowCloseButton];
[windowButton setEnabled:NO];
[self centerOnMainWindowScreen];

[self setupLeftPanel];
[self setupRightPanel];
[self setupConstraints];

Any help, that will explain why is this happening (what is the order of drawing window elements?) and how to properly init window, appreciated. Once again: it's working fine on 10.10 both ways. 10.11 is causing corrupted drawing.

2
I think the darkened title bar is caused by the background color, not by the layer.Sulthan
Have you tried leaving the content view alone and instead adding a subview that fills it and making that layer-backed?Ken Thomases
@Sulthan - I tried changing color ([self setBackgroundColor:]) to black and white and not setting color. Bar is normal, when contentView is not layer-backed.dobry
@KenThomases - Yes I did. I created new NSView and chained it to contentView (addSubiew). Behaviour is identical - corners on bottom not rounded - dark title bar when [intermediatelayer setWantsLayer:YES].dobry
Could you create a Minimum Complete and Verifiable Example ? I tried your code in my window but everything behaved correctly. The only change in title bar was caused by the background color (black background made the whole window dark).Sulthan

2 Answers

0
votes

After all, you guys were right. I wasn't able to create minimum complete and verifiable example, but I decided to change background color of the window to transparent color. I don't know why it was initially set to grey (it was grey when I got the task) but changing it to transparent is not affecting any components put on it later.

So this is a solution for me, but still, I wonder, why is title bar affected when window's contentView is layer backed.

0
votes

I don't have a solution, but I can reproduce this reliably (most recently on macOS 10.12.2), so it's not just you. Save and run the below with swift:

import Cocoa

let window = NSWindow()
window.setContentSize(NSSize(width: 100, height: 100))
window.center()

let layeredView = NSView(frame: window.contentView!.frame)
layeredView.autoresizingMask = [.viewWidthSizable, .viewHeightSizable]
layeredView.wantsLayer = true
layeredView.layer!.backgroundColor = NSColor.blue.cgColor
window.contentView!.addSubview(layeredView)

window.makeKeyAndOrderFront(nil)

NSApplication.shared().run()