Skip to content

Instantly share code, notes, and snippets.

@Arclite
Created November 19, 2022 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arclite/f6829a71dc674ee56e005ae302c83f91 to your computer and use it in GitHub Desktop.
Save Arclite/f6829a71dc674ee56e005ae302c83f91 to your computer and use it in GitHub Desktop.
Creating a transparent window in SwiftUI
// requires Introspect package: https://github.com/siteline/SwiftUI-Introspect
extension View {
private func introspectView(customize: @escaping (NSView) -> ()) -> some View {
return inject(AppKitIntrospectionView(
selector: { introspectionView in
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
return nil
}
return viewHost
},
customize: customize
))
}
private func introspectTitlebarView(customize: @escaping (NSView) -> ()) -> some View {
return inject(AppKitIntrospectionView(
selector: { introspectionView -> NSView? in
guard let viewHost = Introspect.findHostingView(from: introspectionView),
let superview = viewHost.superview
else { return nil }
let subviews = superview.subviews
for subview in subviews {
if NSStringFromClass(type(of: subview)).contains("NSTitlebarContainerView") {
return subview
}
}
return nil
},
customize: customize
))
}
public func hideTitlebar() -> some View {
return self
.introspectView { view in
guard let window = view.window else { return }
window.isOpaque = false
window.backgroundColor = .clear
}
.introspectTitlebarView { titleBarView in
print(titleBarView)
titleBarView.isHidden = true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment