Nswindow with Round Corners in Swift

NSWindow with round corners in swift

Also add this code to your code

window.isOpaque = false
window.backgroundColor = .clear

I don't have my machine to check but once I had the same issue and I think I resolved it by using above code. I will check when I get back to my machine.

Give it a shot and update me.

When exactly does an NSWindow get rounded corners?

The flag to set, in order to get NSWindow to clip its contents to its actual window border (why the heck isn't this the default?) is NSView's wantsLayer.

  • In code: contentView!.wantsLayer = true, when the window is loaded.

  • Or in IB: select the root View of your Window, and then in "View Effects inspector", check the box next to it in "Core Animation Layer" (even if you're not using Core Animation).

Meta: the documentation for wantsLayer talks about a lot of complex things that don't seem at all related to window shape or clipping, and it's not even a property of NSWindow, while NSWindow has some flags that claim to be about window corner rounding but which don't work any more, so I'm not sure how anybody is supposed to discover this, except by spending 2 days on trial-and-error. I hope this answer helps somebody else!

SwiftUI for MacOS Window with Rounded Corners without Title Bar

...create a corner radius on the view but the window is still square.

You need to remove window background

window.backgroundColor = NSColor.clear

and, btw, add full content size

searchWindow = SWindow(
contentRect: NSRect(x: 0, y: 0, width: 850, height: 500),
styleMask: [.resizable, .fullSizeContentView],
backing: .buffered, defer: false)

Modal Panel with rounded corners

After working a lot on it, I finally found a solution:

Frist create a new borderless window in your interface builder. Place a custom box in that view and make sure it leaves a bit of space from the top border of the window:

Sample Image

Then add an outlet of that window object in your app delegate:

@IBOutlet weak var saveWindow: NSWindow!

So copy that extension for loading and dismissing a panel as a modal sheet:

extension NSWindow {

public func loadPanel(named: NSWindow) {
named.isOpaque = false
named.backgroundColor = NSColor.clear
named.hasShadow = false
self.beginSheet(named, completionHandler: nil)
}
public func closePanel(named: NSWindow) {
self.endSheet(named)
}

}

You just need to call this two functions if you want to open a panel.

window.loadPanel(named: saveWindow)

And when you're done:

window.closePanel(named: saveWindow)

This is the result:

Sample Image

How to make a window with rounded corners

The problem with the label is you're adding NSVisualEffectView above it. You could instead try adding it below:

view.addSubview(visualEffect, positioned: .below, relativeTo: label1)

But be careful you add it only once: viewWillAppear can be called multiple times.

Borderless NSWindow with rounded corners

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.

How can I round the corners of only one side of an NSButton in Swift 4?

1) The images you showed are using a simple NSSegmentedControl. Nothing needs customized.

2) What you tried to do wouldn't work anyway; If it could mechanically work, what it would end up doing is merely clipping the drawn content on the left corners. It wouldn't magically fill in drawing on the right, and create appropriate control borders etc.

AppKit controls are not merely CALayers with filled in properties like border, background, etc. They are almost all entirely drawn using Core Graphics via the classic drawRect: method one way or another. The fact that views have a layer is due to layer-backing. There are very few things you can end up doing with the layer of an existing control. To customize them properly, you would override the standard drawing routines in NSView, NSControl, NSCell, etc as appropriate.



Related Topics



Leave a reply



Submit