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 swift September 13, 2024 How to add a character limit to a TextField in SwiftUI?

To add a character length limit to a TextField in SwiftUI, you can use a combination of Swift’s .onChange, .onPasteCommand modifier, and string manipulation to limit the number of characters the user can enter.

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.

question swiftui April 2, 2024 How to declare an array of Views in SwiftUI?

In SwiftUI, you can declare an array of View using the standard Swift array syntax. Here’s how you can do it: