setBackgroundColor covers element - objective-c

I have an NSWindow, and I'm using this code to add a bottom-metal-bar at the bottom.
[MyWindow setContentBorderThickness:40.0 forEdge:NSMinYEdge];
That works fine. But, once I use this:
[MyWindow setBackgroundColor: [NSColor redColor]];
The red covers the bar at the bottom. The bar shows correctly without the background color.

Yes, it would appear that changing the background-color of an NSWindow negates its bottom border. In order to achieve both effects, you can do one of two things:
In Interface Builder, move all your interface elements to a subclass of NSView that draws its background and add the view to your window.
Create an NSView that emulates the bottom border of your window and set the window's background color.
Personally, I would go for the first option, because it requires less work (trying to emulate a bottom border will be difficult, even with NSGradient) , but both are a possibility.

Related

How do you use the dark vibrancy on an NSWindow?

What is the correct way to use the Vibrant Dark (NSAppearanceNameVibrantDark) or Vibrant Light (NSAppearanceNameVibrantLight) modes with an NSWindow?
I'm building an application and would like to offer the new Vibrant Dark appearance as an option for the main application's NSWindow. The window itself is a pretty straight forward window with an NSToolbar across the top and a scroll view as the main content area.
There's plenty of information from Apple on using the new vibrancy appearances in conjunction with an NSVisualEffectsView, but I'm looking for clarification on how to use those appearances on an NSWindow.
In the NSAppearance.h header file, there's a comment that states that the vibrant appearances should only be set on an NSVisualEffectsView. There's no mention of it being supported on NSWindow.
The WWDC videos talk about making sure that you're using layer-backed views and that you're allowing vibrancy on your subviews when using the new vibrant appearances, but again, no mention of using them on an NSWindow.
The VisualEffectsPlayground sample code does have an example of a Facetime-like application that uses the dark vibrancy mode, but it doesn't have a toolbar and it uses the full-content mask.
From that information, I'm doing the following in my NSWindowController's windowDidLoad method:
[self.window.contentView setWantsLayer:YES];
self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
And, sure enough, I get a black NSToolbar with a black window title bar. So it looks like it's working properly, but when my scrollview scrolls its content (thumbnails of images) the standard translucency under the NSToolbar and the window's title bar is either not present or is randomly present. There doesn't seem to be any pattern. Sometimes when I scroll the scrollview, the content is shown in the "blurred" state under the toolbar and title bar. Other times when I scroll, the toolbar and titlebar are just an opaque black. (The title bar and toolbar still draw their titles and buttons.)
When I don't request a layer for the window's content view, then I get more a gray titlebar and window title rather than a "pure" black one. (My scrollview's background color can be either white, dark gray or black.)
Any help or clarification on how to properly configure an NSWindow (that contains an NSToolbar and an NSScollView) to use the new vibrancy appearances would be much appreciated.
This will make a dark mode window. Subsequently, everything in the window (including titlebar, toolbar and even dialogue sheets) will become dark.
let USE_DARK_MODE = true
if USE_DARK_MODE {
window.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
window.invalidateShadow()
}
invalidateShadow doesn't seem required, but sometimes shadow does not work properly if lacked. (OS X 10.10)
I figured that you can subclass NSWindowController. Set the class for your Window Controller in IB to your custom NSWindowController. After that in the windowDidLoad function
self.window?.titlebarAppearsTransparent = true
Also set "Full Size Content View" in the Attribute inspector for the NSWindow.

Cocoa: NSOutlineView not highlighted as blue bar

I use the - addSubview: method to add an subview with outline view.
But I found strange that if I programmatically select an object of this outline view, the selection highlighting was not blue:
However, as long as I perform an mouse click on the outline view, its highlighting became normal:
Why did this happen? How can I make the highlighting drawn as blue before any mouse event?
Thank you at advance!
If the outline view doesn't have focus it will use the grey color. Make it first responder, to get focus, and the selection turns blue.
You can make it first responder for example by using NSWindows makeFirstResponder: :
[self.window makeFirstResponder:self.outlineView];

Transparent NSWindow but with standard border and shadow [duplicate]

This question already has an answer here:
Holes in NSView or NSWindow
(1 answer)
Closed 6 years ago.
I want to have a more or less standard NSWindow with a toolbar and all that, but I want the content view to be transparent so that I can see through it. At the same time I want to keep the light gray outline of the window and also it's shadow. BUT I want to avoid the "inner" shadow I get from the toolbar inside the content view area.
What I have tried so far is just to set the window background color to a semi transparent color and also set opaque to NO. The problem is that the window border fades away with the alpha of the background itself, and the more transparency I have on the background, the more the shadow of the toolbar shows up within the content view.
Generally, the window shadow and border changes depending on the transparency of the content view, which I totally understand. But I want a behavior where it keeps the border and shadow just as if it was a completely opaque window, and then I want the content view area to be transparent.
I am not sure what I need to do conceptually to make it work. Maybe I have to draw the window border myself, maybe not. Maybe I need to draw the shadow myself, or maybe not.
Is there anyone that know how to build this? I don't need exact code details, but rather what parts I need to do custom..
I appreciate any input!
I dont't know if this is of any value for you after all this time but try:
[aWindow setOpaque:NO];
[aWindow setBackgroundColor:[NSColor clearColor]];
Subclass the NSView class, override the drawRect:(NSRect)dirtyRect method and set the color of the view as clearcolor, now set the class of your content view as the Subclass of NSView.

Borderless NSWindow with rounded corners

I am creating a custom NSWindow with no title bar and am using NSBorderlessWindowMask to make it completely borderless. The problem I have with this however is that the window has sharp edges. As well as this there is no resize control.
How would I give a borderless window rounded corners?
This is not a duplicate of this
question as that question was more
about removing the title bar and it
currently holds no answers.
You can make the window totally transparent and handle drawing everything yourself. The sample I have is for an OpenGL view, but it should work for a Quartz view or Cocoa view as well.
Add the following to the initializer of your NSWindow subclass where you create the new window using the NSBorderlessWindowMask constant.
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
You will probably have to draw the resize control yourself. The sample I took this from is a full screen window so resizing isn't necessary.
Good Luck.
The easiest way to get a window with rounded corners is to place a NSBox into the window as these boxes have customizable rounded corners and customizable borders. If you then set the window to non-opaque and the background color to transparent ("clear color"), you have a NSWindow with rounded corners that draws a normal window shadow (even on older systems where such a window would otherwise not have a shadow). Most of it can be done in Interface Builder. See here for details.
Only titled windows get the rounded corners. So the only thing you have to do is this:
window.styleMask = [.titled]
window.titleVisibility = .hidden
window.titlebarAppearsTransparent = true
This should be the minimal configuration for a rounded window without a title bar.

Dimming NSWindow and layer NSView on top

Is there any way I could dim my NSWindow (basically putting a black transparent layer over top of it that you cannot click through) and then layer a custom NSView on top?
You could create an additional NSView which contains your custom NSView. This new NSView would simply draw the black transparent layer in its drawRect and capture all events to avoid them being passed through to the window.
A more compartmentalized way would be to make a borderless window containing the black view and the custom NSView, and make that a child window of the window you want to “dim”. Then use NSViewAnimation to fade the window in and out.
You will, of course, need to handle keeping their sizes matched if the “dimmed” window is resizable.
You could just set the background color of your NSWindow the black and change the opacity to your liking.