Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Created January 18, 2021 16:22
Show Gist options
  • Save OskarGroth/8f5500201cd94bbb10a29eb16b6eacee to your computer and use it in GitHub Desktop.
Save OskarGroth/8f5500201cd94bbb10a29eb16b6eacee to your computer and use it in GitHub Desktop.
import SwiftUI
class Coordinator: ObservableObject {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
init() {
let view = NSHostingView(rootView: Text("Hello from SwiftUI").fixedSize())
item.button?.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(equalTo: item.button!.leadingAnchor),
view.trailingAnchor.constraint(equalTo: item.button!.trailingAnchor),
view.heightAnchor.constraint(equalToConstant: 20),
view.centerYAnchor.constraint(equalTo: item.button!.centerYAnchor)
])
}
}
struct ContentView: View {
@StateObject var coordinator = Coordinator()
var body: some View {
Text("Check your status bar!")
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment