问题描述
我已经使用NSVisualEffectView
创建了一个窗口以获取模糊和圆角.就像此处
I have created a window using NSVisualEffectView
to get blur and rounded corners. Like here
问题是当我拥有NSVisualEffectView
代码时,我在窗口中看不到我的按钮.如果删除代码,则会显示该按钮.怎么了?
The problem is I don't see my button in the window when I have NSVisualEffectView
code. If I remove the code, the button is displayed. What is going wrong?
AppDelegate.swift中的NSVisualEffectView代码:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
guard let window = NSApplication.shared().windows.first else { return }
let effect = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
effect.blendingMode = .behindWindow
effect.state = .active
effect.material = .dark
effect.wantsLayer = true
effect.layer?.cornerRadius = 15.0
effect.layer?.masksToBounds = true
window.isOpaque = false
window.backgroundColor = .clear
window.contentView = effect
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
}
我在情节提要中添加了一些按钮.当我运行项目时,我没有看到任何按钮.
I have added some buttons in storyboard. When I run the project I don't see any buttons.
当我从applicationDidFinishLaunching(_ aNotification: Notification)
中删除所有内容时,即NSVisualEffectView
代码,我可以看到按钮.
When I remove everything from applicationDidFinishLaunching(_ aNotification: Notification)
i.e., NSVisualEffectView
code, I can see the buttons.
谁能告诉我发生了什么事?
Can anyone tell me what is happening?
推荐答案
我认为我应该在您的,但我没有.
I think I should have corrected you in your previous question only but I didn't.
您正在使用Storyboard,所以为什么要在代码中创建NSVisualViewEffect
变量?
You are using Storyboard so why are you creating NSVisualViewEffect
variable in your code?
在右侧面板(实用程序面板)中搜索nsvisualeffectview
,在此处搜索按钮等(对象库).
Search for nsvisualeffectview
in the right panel(Utilities panel) where you search for buttons etc. (object library).
拖动它并根据您的主视图控制器调整其大小.
Drag it and resize it according to your main view controller.
要添加模糊效果和模式,请转到实用工具"面板中的属性检查器"
To add the blur effect and mode, go to "Attribites Inspector" in the "Utilities panel"
并设置window.backgroundColor = .clear
和window.isOpaque = false
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
guard let window = NSApplication.shared.windows.first else { return }
window.isOpaque = false
window.backgroundColor = .clear
}
现在,您可以添加按钮,文本字段并运行项目.您可以看到所有添加的元素.
Now you can add your buttons, text fields and run the project. You can see all your added elements.
希望对您有帮助!
这篇关于使用NSVisualEffectView时如何添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!