Chapters

Hide chapters

Catalyst by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

10. Barista Training: Menu Bar
Written by Marin Bencevic

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Welcome to barista training! In the next few chapters, you’ll learn all about adding different kinds of bars to your Catalyst app, including the menu bar, toolbars and supporting the touch bar. In this chapter, you’ll trim the default menu bar of the Journalyst app to remove some unnecessary items. You’ll also add new items to delete, share and add new entries. Get out your beans and your fancy hipster oat milk — it’s time to get brewing!

A free menu bar

All Catalyst apps include a default menu bar for free. Open up the starter project from the provided materials and run it on macOS. When your app is active, you’ll see its menu bar at the top of your screen. All the standard menus like File, Edit, etc. are already there.

Apple sometimes calls the menu bar the “main menu.” That’s how you should think about what actions to put in the menu bar. Like in a video game, the main menu includes general actions the user can perform inside your app.

One thing you should keep in mind is that menu bars aren’t dynamic. They’re built once when the app launches and the items never change during runtime. For more dynamic actions related to specific parts of your app, use context menus as described in Chapter 5, “Adding Some Context.”

The menu bar itself is a nested UIMenu instance. Each UIMenu can contain child menus and commands. Commands are the buttons you can press to do something. They can be enabled or disabled, and each command can specify a keyboard shortcut for easier access.

Commands get executed using something called the responder chain.

The responder chain

Open the Edit menu, and you’ll notice that Cut, Copy and Paste are all greyed out. This makes sense: Since nothing is selected, there’s nothing to cut or copy.

Beyond the default menu

You’ll start by trimming some unnecessary items from the menu bar. The menu bar can be changed either in Interface Builder or through code. In this section, you’ll use Interface Builder and, later in the chapter, you’ll learn how to do the same in code.

Deleting entries

Back in Main.storyboard, select Item 2 in the File submenu you added earlier. In the Attributes Inspector, change its Title to Delete Entry and set the Key Equivalent to Shift-Command-Backspace.

@IBAction func deleteEntry(_ sender: Any) {
  guard let selectedIndexPath = 
    mainViewController?.tableView.indexPathForSelectedRow else {
      return
  }
  
  DataService.shared.removeEntry(atIndex: selectedIndexPath.row)
  if DataService.shared.allEntries.isEmpty {
    DataService.shared.addEntry(Entry())
  }
  mainViewController?.showEntry(
    at: IndexPath(row: 0, section: 0))
}

// 1
override func validate(_ command: UICommand) {
  // 2
  switch command.action {
  case #selector(deleteEntry):
    // 3
    if let selectedIndexPath = 
      mainViewController?.tableView.indexPathForSelectedRow {
      let entry = 
        DataService.shared.allEntries[selectedIndexPath.row]
      command.title = "Delete \(title(for: entry))"
    } else {
      // 4
      command.title = "Delete Entry"
    }
  default:
    break
  }
}

Sharing entries

The menu bar is only one of the potentially many menus you can have in your app. Each UIResponder can add or remove items from their menus. The responder that’s responsible for the menu bar is the application itself, or in other words, the app delegate.

override func buildMenu(with builder: UIMenuBuilder) {
}
guard builder.system == .main else { return }
let shareCommand = UIKeyCommand(
  title: "Share",
  action: #selector(RootSplitViewController.share),
  input: "s",
  modifierFlags: [.command])
let shareMenu = UIMenu(
  title: "",
  options: [.displayInline],
  children: [shareCommand])
builder.insertChild(shareMenu, atStartOfMenu: .file)

case #selector(share):
  if (entryViewController?.entry?.log ?? "").isEmpty {
    command.attributes = [.disabled]
  } else {
    command.attributes = []
  }

Key points

  • Catalyst apps include a default menu bar for free.
  • The menu bar consists of nested menus which contain commands.
  • Each command has a selector it calls when pressed, and uses the responder chain to enable and disable itself.
  • You add or remove items from the menu bar by dragging over a Main Menu to your app’s storyboard.
  • You can make the same changes in code by overriding buildMenu in the app delegate.
  • Override validate in a UIResponder subclass to change the appearance of a command.

Where to go from here?

The Human Interface Guidelines section on menus (https://apple.co/2YvlNH9) has some useful tips of which actions to consider for the menu bar, and where to put them.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now