Skip to content

Instantly share code, notes, and snippets.

@chriszielinski
Last active February 8, 2024 23:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriszielinski/359ca5ef45d2d652dfb8857aa127136b to your computer and use it in GitHub Desktop.
Save chriszielinski/359ca5ef45d2d652dfb8857aa127136b to your computer and use it in GitHub Desktop.
NSViewController extension for fetching a window title's NSTextField
extension NSViewController {
var titlebarTextField: NSTextField? {
// NSTitlebarContainerView
guard let titlebarContainerView = view.superview?.subviews
.first(where: { $0.className.hasSuffix("ContainerView") })
else { return nil }
// NSTitlebarView
guard let titlebarView = titlebarContainerView.subviews
.first(where: { $0.className.hasSuffix("TitlebarView") }) as? NSVisualEffectView
else { return nil }
// NSTextField
return titlebarView.subviews
.first(where: { $0 is NSTextField }) as? NSTextField
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment