# SwiftUI 点击未激活的 NSWindow 上的按钮时,默认让按钮处理鼠标事件,而不是窗口
NSView 重载 acceptsFirstMouse (opens new window) 方法,始终返回 true
。
SwiftUI 的视图里 overlay 添加这个重载的 View。
import SwiftUI
import Cocoa
// Just mouse accepter
class MyViewView : NSView {
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
}
// Representable wrapper (bridge to SwiftUI)
struct AcceptingFirstMouse : NSViewRepresentable {
func makeNSView(context: NSViewRepresentableContext<AcceptingFirstMouse>) -> MyViewView {
return MyViewView()
}
func updateNSView(_ nsView: MyViewView, context: NSViewRepresentableContext<AcceptingFirstMouse>) {
nsView.setNeedsDisplay(nsView.bounds)
}
typealias NSViewType = MyViewView
}
// Usage (somewhere in your SwiftUI view stack)
Text("Click me")
.padding(20)
.background(Color.yellow)
.overlay(AcceptingFirstMouse()) // must be on top (no confuse, it is transparent)
.onTapGesture {
print("Label tapped")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
作者:Sintone Li
文章地址:https://cleanclip.cc/zh/developer/swiftui-nswindow-inactive-firstmouse/