Home iOS & Swift Books Catalyst by Tutorials

10
Barista Training: Menu Bar Written by Marin Bencevic

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.

@IBAction @objc private func addEntry(sender: UIKeyCommand) {

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 @objc private func removeEntry(sender: UIKeyCommand) {

// 1
override func validate(_ command: UICommand) {
  // 2
  switch command.action {
  case #selector(removeEntry):
    // 3
    if let mainNavigationController = viewController(for: .primary) 
      as? UINavigationController,
      let mainTableViewController = mainNavigationController.topViewController 
      as? MainTableViewController,
      let selectedIndexPath = mainTableViewController.tableView.indexPathForSelectedRow {
      // 4
      let entry = DataService.shared.allEntries[selectedIndexPath.row]
      command.title = "Delete \(entry.dateCreated)"
    } 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(EntryTableViewController.share),
  input: "s",
  modifierFlags: [.command])
let shareMenu = UIMenu(
  title: "",
  options: [.displayInline],
  children: [shareCommand])
builder.insertChild(shareMenu, atStartOfMenu: .file)

override func validate(_ command: UICommand) {
  switch command.action {
  case #selector(share):
    if textView.text.isEmpty {
      command.attributes = [.disabled]
    } else {
      command.attributes = []
    }
  default:
    break
  }
}

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 that 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 (apple.co/2EymXZv) 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.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2020 Razeware LLC

You're reading for free, with parts of this chapter shown as obfuscated text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.