---
title: "#281: Touch Bar Basics 🚥"
url: https://littlebitesofcocoa.com/281-touch-bar-basics
author: Jake Marsh
published: 2016-10-28
topics: ["AppKit","Touch Bar"]
description: "During yesterday's MacBook Pro event, Apple announced a fantastic new piece of hardware called the Touch Bar. It's a 1085 x 30 point matte-finish Retina..."
---

# #281: Touch Bar Basics 🚥

By Jake Marsh, October 28, 2016. HTML: https://littlebitesofcocoa.com/281-touch-bar-basics

During yesterday's **MacBook Pro event**, Apple announced a fantastic new piece of hardware called the _Touch Bar_. It's a `1085` x `30` point matte-finish Retina screen that sits above the keyboard on the new **MacBook Pros**. It can dynamically alter its controls and content as we work in each app on the main screen.

Today we'll look at the basics of adding _Touch Bar_ support to a **macOS app**. Let's dive in.

First some pre-requisites. You'll need the latest version of **Xcode** (8.1 at publish time), and the latest version of **macOS**. Even for those running 10.12.1, [this updated 10.12.1 build](https://support.apple.com/kb/dl1897?locale=en_US) still needs to be installed to be able to test _Touch Bars_ using **Xcode**.

After we've made sure we've got the latest versions of everything, we can launch Xcode 8.1 and select **Window** > **Show Touch Bar** to see a simulated _Touch Bar_ on screen while we're work on our **Mac**.

It even floats above all other **windows** to make it feel a tiny bit more realistic. Neat!

<img src="https://media.littlebitesofcocoa.com/281-1.png" style="max-width: 387px; border-radius: 4px;" />

<img src="https://media.littlebitesofcocoa.com/281-2.png"  style="max-width: 602px;" />

Whew! With that out of the way, we can fire up **Xcode** and create a new **macOS app**.

We'll make a new `WindowController` for our **app's** initial **window**, and set its **class** in **Interface Builder**.

``` swift
// WindowController.swift
class WindowController: NSWindowController {
  override func windowDidLoad() {
    super.windowDidLoad()

  }
}
```

<img src="https://media.littlebitesofcocoa.com/281-3.png" style="max-width: 684px; border-radius: 4px;" />

We'll add an **extension** to `WindowController` to adopt the `NSTouchBarDelegate` protocol. We'll also add an `@available` statement to make sure it's only available on the latest **macOS**:

``` swift
@available(OSX 10.12.1, *)
extension WindowController : NSTouchBarDelegate {
  override func makeTouchBar() -> NSTouchBar? {
    // TODO
  }
  
  func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
    // TODO
  }
}
```

Next, we need to create our _Touch Bar_. We'll do this with an instance of `NSTouchBar`. 

``` swift
override func makeTouchBar() -> NSTouchBar? {
  let touchBar = NSTouchBar()
  touchBar.delegate = self
  return touchBar
}
```

We'll use another **extension** to add some **identifiers** for the _Touch Bar Items_ we want to appear to `NSTouchBarItemIdentifier`:

``` swift
extension NSTouchBarItemIdentifier {
  static let launch = NSTouchBarItemIdentifier("com.magnus.spaceships.launch")
  static let hyperspace = NSTouchBarItemIdentifier("com.magnus.spaceships.hyperspace")
}
```

Then, we'll set them in the order we want on our `touchBar's` `defaultItemIdentifiers` **property**:

``` swift
touchBar.defaultItemIdentifiers = [.launch, .hyperspace]
```

Then, we'll implement the sort of "main" **function** of `NSTouchBarDelegate` (The <i>"cell for item at index path"</i> of _Touch Bars_ so-to-speak).

We'll `switch` on our **identifiers** from earlier, and return an `NSTouchBarItem` for each one:

``` swift
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
  switch identifier {

  case NSTouchBarItemIdentifier.launch:
    let item = NSCustomTouchBarItem(identifier: identifier)
    item.view = NSButton(title: "🚀 Launch", target: self, action: #selector(launch))
    return item

  case NSTouchBarItemIdentifier.hyperspace:
    let item = NSCustomTouchBarItem(identifier: identifier)
    item.view = NSButton(title: "😱 Hyperspace", target: self, action: #selector(hyperspace))
    return item

  default: return nil
  }
}
```

Success!

<img src="https://media.littlebitesofcocoa.com/281-4.png"  style="max-width: 344;" />

Last but not least, we can allow our _Touch Bar_ items to be customized by assigning it a `customizationIdentifier`:

``` swift
touchBar.customizationIdentifier = NSTouchBarCustomizationIdentifier("com.magnus.spaceships")
```

``` swift
if #available(OSX 10.12.1, *) {
  if ((NSClassFromString("NSTouchBar")) != nil) {
    NSApplication.shared().isAutomaticCustomizeTouchBarMenuItemEnabled = true
  }
}
```

We'll also need to define which items can be customized:

``` swift
touchBar.customizationAllowedItemIdentifiers = [.launch, .hyperspace]
```

We can test out customization by selecting **View** > **Customize Touch Bar...** from our **app's menu**.

(**Pro Tip:** Hold **Option** to change the **menu item** to **Customize Control Strip...** instead, which allows us to customize the persistent `NSTouchBarItems` that are always available on the right side of the _Touch Bar_). Neat!

<p></p>
