0
votes

I could find a good solution to make a window with rounded corners

https://github.com/lukakerr/NSWindowStyles Section: 6. Vibrant background with border radius and no titlebar

let visualEffect = NSVisualEffectView()
visualEffect.translatesAutoresizingMaskIntoConstraints = false
visualEffect.material = .dark
visualEffect.state = .active
visualEffect.wantsLayer = true
visualEffect.layer?.cornerRadius = 16.0

window?.titleVisibility = .hidden
window?.styleMask.remove(.titled)
window?.backgroundColor = .clear
window?.isMovableByWindowBackground = true

window?.contentView?.addSubview(visualEffect)

guard let constraints = window?.contentView else {
  return
}

visualEffect.leadingAnchor.constraint(equalTo: constraints.leadingAnchor).isActive = true
visualEffect.trailingAnchor.constraint(equalTo: constraints.trailingAnchor).isActive = true
visualEffect.topAnchor.constraint(equalTo: constraints.topAnchor).isActive = true
visualEffect.bottomAnchor.constraint(equalTo: constraints.bottomAnchor).isActive = true

I find that it only works if I put it in the viewWillAppear or in the viewDidLoad with a delay. In any case, when I get the border-radius and the vibrant background I cannot see anything that is in the window, for instance, a simple label with the text test. (I tried to put that text on the storyboard or by code)

@IBOutlet weak var label1: NSTextField!

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
   self.label1.stringValue = "test text"
}

How to make a label visible with this code?

(or as second option, how to make a window with round corners, no bar, background semi-transparent or vibrant and a label non-transparent on top or inside?)

(I know how to make the title bar transparent and remove the buttons and title. But that is not a good solution for what I need because the bar is still there and creates a space that makes problems)

1
could you not offest the content up so that the "space" the title bar takes up appears gone?Julian Silvestri
@JulianSilvestri I have tried that and the solution I explain here would be much better If I could solve the problem with the labelNrc

1 Answers

0
votes

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.