Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

How to Use NSTouchBar on macOS

Learn how to use the new NSTouchBar APIs to add Touch Bar support for your macOS apps.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/883-how-to-use-nstouchbar-on-macos

“Show TouchBar” is missing.

Hi, I have MacOS Sierra 10.12.1 (16B2657) with the correct build and Xcode 8.1, but, I do not
see the “Show TouchBar” option on the “Window” menu in Xcode. Any ideas? I am on an iMac.

Great tutorial, thanks for getting it up so quick!

Chris

Hey @cbcdiver,

I had to completely reinstall Xcode 8.1 on one of my machines to get it working there. I noticed it was because I had installed Xcode on 10.11, then upgraded to 10.12, then 10.12.1. Not sure if you had the same situation, but it’s worth trying a fresh Xcode install. If not… I wish I could tell you more.

Thanks Andy. That worked! :wink:

Chris

Excellent tutorial.

However, I would like to add a global key to the TouchBar, something that will persist throughout app changes.
Do you know of any documentation for this, or can you point me in the right direction?

Xcode adds a custom button to the “control center” region, so there must be a way to do it.

Thanks

@cbcdiver @macandyp I noticed that Cocoa calls makeTouchBar() in the Window Controller level, not in the View Controller. In the tutorial it is written in the view controller, and what is missing is that you have to provide a custom Window Controller to implement makeTouchBar(). In writer’s case, he passed in the result of underlying view controller’s makeTouchBar method.

I tried for 2 hours by myself and finally discovered why my touch bar was empty all the time.

I don’t think it’s a good idea to use if #available(OSX 10.12.1, *) { to check for support. Like you mentioned, pre-16B2657 builds don’t support it.

I actually submitted an app for review and the reviewer was using 10.12.1 16B2555 - and so it was crashing for him/her.

I’m changing app now to if #available(OSX 10.12.2, *) { while still wondering if there’s any better way to check for touch bar support. Maybe NSClassFromString("NSTouchBar") != nil ?

Might be good to mention that to have the touchbar showing when the focus is in the textfield, you have to disable the character picker:

nameField.allowsCharacterPickerTouchBarItem = false

@vojto I read there explicitly isn’t an API available to test the presence of the touchbar.

1 Like

@koenpunt Yeah that’s what I figured too.

Anyway, changing from if #available(OSX 10.12.1, *) to if #available(OSX 10.12.2, *) made it pass the review. The reviewer was using 10.12.1 without touch bar support. Keep in mind there will be a small number of users with this specific version and they’ll be seeing unexpected crashes.

@djben

could you post your WindowController fix please? I cannot work out how to get the example to work. Or can anyone update the example project so that it will show the touchbar?

https://github.com/DJBen/TouchbarTalk

Here’s a very simple demo I made just to show a text field.

Thank-you for your response. But I downloaded the project and the .swift files (i.e. ViewController, WindowController) are all pretty much empty. Am I missing something?

Sorry, forgot to push. It’s updated.

Thank-you very much. It works. I can see what needs to be done.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]