Created
January 18, 2021 16:22
-
-
Save OskarGroth/8f5500201cd94bbb10a29eb16b6eacee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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