如何制作带有圆角的窗口 [英] How to make a window with rounded corners

查看:63
本文介绍了如何制作带有圆角的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到一个好的方法来制作带有圆角的窗口

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

https://github.com/lukakerr/NSWindowStyles 部分:6.充满活力的背景,带有边框半径,没有标题栏

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

我发现只有将其延迟放置在viewWillAppear或viewDidLoad中,它才有效.无论如何,当我获得边框半径和充满活力的背景时,我看不到窗口中的任何内容,例如带有文本测试的简单标签.(我试图将这些文本放在情节提要上或通过代码放置)

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)

推荐答案

标签的问题是您要在其上方添加 NSVisualEffectView .您可以尝试在下面添加它:

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)

但是请注意,您只能添加一次: viewWillAppear 可以多次调用.

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

这篇关于如何制作带有圆角的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆