Build a To-Do Menu Bar App in SwiftUI
Some apps deserve a big window. A to-do list for the next hour does not.
In this tutorial we'll build TodoBar: a tiny to-do list that lives in the macOS menu bar. Click the icon, type a task, press Return, tick it off later. No Dock icon, no main window, no fuss.
It's about 140 lines of Swift across four small files, and by the end you'll have used:
MenuBarExtra, SwiftUI's built-in menu bar scene- an
@Observablemodel that the view reacts to CodableandUserDefaultsto remember your list between launches- one Info.plist key that hides the Dock icon
Here's the plan:
click the icon → type a to-do → press Return → it's saved → tick it off → feel great
Note
You'll need Xcode 15 or newer and macOS 14 (Sonoma) or newer. MenuBarExtra arrived in macOS 13, and the @Observable macro we use for the model needs macOS 14.
Step 1: Create the Project
Open Xcode and choose File › New › Project…, then:
- Pick macOS › App and click Next.
- Name it
TodoBar. - Set Interface to SwiftUI and Language to Swift. Leave Storage on None.
- Save it wherever you keep your projects.
Xcode gives you two files: TodoBarApp.swift and ContentView.swift. We won't need ContentView.swift, so delete it (Move to Trash). We're about to replace the app file anyway.
While you're in the project settings, check Minimum Deployments on the TodoBar target's General tab and make sure it's macOS 14.0 or later.
When we're done, the project will have four Swift files:
TodoBar/
├── TodoBarApp.swift // the app and its menu bar icon
├── TodoItem.swift // one to-do
├── TodoStore.swift // the list, plus saving and loading
└── TodoListView.swift // what you see when you click the icon
Step 2: The Model
Every to-do needs three things: something to identify it, a title, and whether it's done.
Create a new Swift file (File › New › File…, then Swift File) called TodoItem.swift:
import Foundation
struct TodoItem: Identifiable, Codable {
var id = UUID()
var title: String
var isDone = false
}
That's the whole model. Two protocols are doing the heavy lifting:
Identifiablegives each item a stableid, so SwiftUI'sForEachcan tell rows apart even when two to-dos have the same title.Codablelets Swift turn an array of these into JSON and back again for free. We'll lean on that for saving.
Step 3: The Store
The store owns the list. Views ask it to add, tick or delete, and it saves after every change.
Create TodoStore.swift:
import Foundation
import Observation
@Observable
final class TodoStore {
private(set) var items: [TodoItem] = []
private let key = "todos"
var remaining: Int { items.filter { !$0.isDone }.count }
init() {
load()
}
func add(_ title: String) {
let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
items.append(TodoItem(title: trimmed))
save()
}
func toggle(_ item: TodoItem) {
guard let index = items.firstIndex(where: { $0.id == item.id }) else { return }
items[index].isDone.toggle()
save()
}
func remove(_ item: TodoItem) {
items.removeAll { $0.id == item.id }
save()
}
func clearDone() {
items.removeAll { $0.isDone }
save()
}
// MARK: Saving
private func save() {
guard let data = try? JSONEncoder().encode(items) else { return }
UserDefaults.standard.set(data, forKey: key)
}
private func load() {
guard let data = UserDefaults.standard.data(forKey: key),
let saved = try? JSONDecoder().decode([TodoItem].self, from: data) else { return }
items = saved
}
}
A few things worth pointing out:
@Observableis the magic sticker. Any SwiftUI view that readsitemsorremainingredraws when they change. No@Published, no Combine.private(set)means views can read the list but can't change it directly. Every change goes throughadd,toggle,removeorclearDone, and every one of those ends withsave(). One door in, one place that saves.addtrims whitespace and ignores empty titles, so pressing Return in an empty field doesn't create a blank to-do.- Saving encodes the whole array to JSON and stores it in
UserDefaultsunder the key"todos". Loading does the reverse when the store is created. If nothing's saved yet (first launch) or the data can't be read, we simply start with an empty list.
Tip
UserDefaults is perfect for a small list like this. If your app grows to thousands of items, attachments or sync, that's the moment to look at SwiftData or a file in Application Support.
Step 4: The Menu Bar Scene
Now replace everything in TodoBarApp.swift with this:
import SwiftUI
@main
struct TodoBarApp: App {
@State private var store = TodoStore()
var body: some Scene {
MenuBarExtra {
TodoListView(store: store)
} label: {
Image(systemName: store.remaining == 0 ? "checkmark.circle.fill" : "checklist")
}
.menuBarExtraStyle(.window)
}
}
This is where it becomes a menu bar app:
- There's no
WindowGroup. A normal SwiftUI app opens a window; ours only declares aMenuBarExtra, so the menu bar icon is the whole app. @State private var storecreates the store once and keeps it alive for as long as the app runs.- The
labelis what sits in the menu bar. It's an SF Symbol that changes to a filled checkmark when everything's done. A tiny reward, but you'll be surprised how satisfying it is. .menuBarExtraStyle(.window)shows our content in a little popover-style panel. The default style,.menu, turns the content into a plain dropdown menu, which can't hold a text field.
Step 5: The List View
The last file is the panel you see when you click the icon. Create TodoListView.swift:
import SwiftUI
struct TodoListView: View {
let store: TodoStore
@State private var newTitle = ""
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("To-Do").font(.headline)
TextField("What needs doing?", text: $newTitle)
.textFieldStyle(.roundedBorder)
.onSubmit {
store.add(newTitle)
newTitle = ""
}
if store.items.isEmpty {
Text("Nothing to do. Go outside! 🌤")
.foregroundStyle(.secondary)
}
ForEach(store.items) { item in
TodoRow(item: item,
onToggle: { store.toggle(item) },
onDelete: { store.remove(item) })
}
Divider()
HStack {
Button("Clear Done") { store.clearDone() }
.disabled(store.remaining == store.items.count)
Spacer()
Button("Quit") { NSApplication.shared.terminate(nil) }
.keyboardShortcut("q")
}
}
.padding()
.frame(width: 280)
}
}
struct TodoRow: View {
let item: TodoItem
let onToggle: () -> Void
let onDelete: () -> Void
var body: some View {
HStack {
Button(action: onToggle) {
Image(systemName: item.isDone ? "checkmark.circle.fill" : "circle")
.foregroundStyle(item.isDone ? .green : .secondary)
}
.buttonStyle(.plain)
Text(item.title)
.strikethrough(item.isDone)
.foregroundStyle(item.isDone ? .secondary : .primary)
Spacer()
Button(action: onDelete) {
Image(systemName: "xmark")
}
.buttonStyle(.plain)
.foregroundStyle(.secondary)
}
}
}
Let's walk through it from top to bottom:
- The text field is bound to
newTitle, a bit of@Statethat only this view cares about..onSubmitruns when you press Return: it hands the text to the store and clears the field, ready for the next one. - The empty state shows a friendly message instead of a blank panel. Small touch, big difference.
ForEach(store.items)draws oneTodoRowper to-do. This works without anid:argument becauseTodoItemisIdentifiable.TodoRowdoesn't know about the store at all. It gets an item and two closures,onToggleandonDelete, and calls them when you click. That keeps the row reusable and easy to preview.- The circle button ticks a to-do on and off. Done items get a green checkmark, a strikethrough and a softer colour.
- Clear Done removes every finished item at once. It's disabled when nothing is done (when
remainingequals the total count). - Quit matters more than it looks. A menu bar app has no Dock icon and no app menu, so without this button the only way to quit is Activity Monitor.
.keyboardShortcut("q")means ⌘Q works while the panel is open.
Notice that store is a plain let. Because TodoStore is @Observable, SwiftUI tracks what the view reads (items and remaining) and redraws when those change. You don't need a property wrapper here.
Step 6: Hide the Dock Icon
Press ⌘R now and the app works, but you'll spot an extra icon in the Dock. A menu bar utility shouldn't have one.
The fix is one Info.plist key:
- Select the TodoBar project in the navigator, then the TodoBar target.
- Open the Info tab.
- Hover over any row under Custom macOS Application Target Properties, click the + button, and choose Application is agent (UIElement).
- Set its value to YES.
Under the hood that's the LSUIElement key. It tells macOS the app is an "agent": no Dock icon, no app menu, and it doesn't show up in the ⌘Tab switcher. Exactly what we want.
Step 7: Run It
Press ⌘R. Look up at the menu bar (not the Dock!) and you'll find a little checklist icon.
Give it a spin:
- Click the icon and type
Water the plants. Press Return. - Add a couple more.
- Tick one off with the circle. Watch it strike through.
- Tick off the rest and look at the menu bar icon. 🎉
- Quit with the Quit button, run it again, and your list is still there. That's
UserDefaultsdoing its job.
Tip
Tired of clicking the menu bar every time you tweak the layout? Add this to the bottom of TodoListView.swift and the panel shows up in Xcode's canvas (Editor › Canvas):
#Preview {
TodoListView(store: TodoStore())
}
Where to Go Next
You've got a working menu bar app. Here are a few easy upgrades, roughly in order of fun per line of code:
- Show a count. Put a
Text("\(store.remaining)")after theImagein thelabel, and the menu bar shows how many to-dos are left next to the icon. - Launch at login. Call
try? SMAppService.mainApp.register()(fromimport ServiceManagement) behind a toggle, so TodoBar starts when you log in. - Edit titles. Swap the row's
Textfor aTextFieldbound to the item, with a store method that saves the change. - Reorder. Put the rows in a
Listand use.onMoveto drag them around. - A global shortcut. Open the panel from anywhere with a keyboard shortcut (the open-source KeyboardShortcuts package makes this painless).
Common Mistakes
The panel is a plain menu and the text field is missing
You're on the default .menu style. Add .menuBarExtraStyle(.window) to the MenuBarExtra.
The app is running but you can't find it
Check the menu bar, not the Dock. If your menu bar is crowded, the icon can hide behind the camera notch on MacBooks; quit another menu bar app or two to make room.
There's no way to quit
Agent apps have no app menu, so you need your own Quit button. Ours is at the bottom of the panel.
Changes don't stick after relaunching
Make sure every method that changes items calls save(). That's why the store keeps items as private(set): nothing else can change the list without going through a method that saves.
Key Takeaways
MenuBarExtraturns a SwiftUI app into a menu bar app; leave outWindowGroupand the menu bar icon is the whole app.- Use
.menuBarExtraStyle(.window)when you want real controls like text fields. - An
@Observablestore withprivate(set)data gives you one place for changes and one place for saving. CodableplusUserDefaultsis plenty of persistence for a small list.LSUIElement(Application is agent) hides the Dock icon, so add your own Quit button.