Loading...

How to render the icon on a menu bar as a template in SwiftUI?

question swiftui
Ram Patra Published on March 6, 2023

Setting your app’s icon on the menu bar as a template makes it to adapt to light and dark modes automatically without you doing anything. To do this, you have to use this constructor of MenuBarExtra class.

The code would look like this:

import SwiftUI

@main
struct ToDoBarApp: App {
    var body: some Scene {
        MenuBarExtra {
            YourView()
        } label: {
            Image("MenuBarIcon").renderingMode(.template)
        }
    }
}
Ram Patra Published on March 6, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui macos October 15, 2023 How to display the app version and build number in a macOS/iOS SwiftUI app?

To display both the app version and build number in a SwiftUI macOS/iOS app, you can use the Bundle class to access information from the app’s Info.plist file. The Info.plist file contains various details about your application, including its version and build number. Here’s how you can do it:

question swiftui swift October 30, 2023 How to convert Color type to hex and vice-versa while retaining alpha information?

The below should work both on macOS and iOS with one minor change. That is, use UIColor instead of NSColor if you’re planning to use it for iOS.