Macos menu bar app swift

The original tutorial was written by Mikael Konutgan. Menu bar apps have been staple of macOS for a long time. Many apps like 1Password and Day One have companion menu bar apps. Fire up Xcode. Then make sure that Swift is selected as the language, and that Use Storyboards is checked. Finally, click Next again, choose a place to save the project and click Create. Once the new project is set up, open AppDelegate. This creates a Status Item — aka application icon — in the menu bar with a fixed length that the user will see and use.

Go to Assets. Select the image and open the attributes inspector. Change the Render As option to Template Image.

Tutorial: Add a Menu Bar Extra to a macOS App

If you use your own custom image, make sure that the image is black and white and configured as a template image so the Status Item looks great against both light and dark menu bars. Back in AppDelegate. This will configure the status item with an icon of the image you just added, and an action for when you click on the item.

Take note of the objc directive in the signature. This exposes the method to the Objective-C runtime to allow the button to use it as an action. Note : If you have too many menu bar apps, you might not be able to see your button. Switch to an app with fewer menus than Xcode like Finder and you should be able to see it. To disable the dock icon, open Info. Build and run.

High five yourself :]. Usually, a measly single action on click is not enough for a menu bar app.Developing for macOS and creating desktop applications is a wonderful process if you want to go down that road. If you are coming from an iOS background like me, then you will find exciting and challenging at the same time to be able to make apps for both systems. What makes it even better is one simple fact: There are lots of common things between iOS and macOS programming.

Several frameworks and SDKs, the language itself Swifteven the coding style that one has used to remain almost the same. But macOS programming is not meant for people being iOS developers already. New programmers who have started learning Swift and they want to make their own desktop applications can follow along as well! Starting from this tutorial we are going to meet and learn key concepts on macOS programming.

I am planning to present various topics for which I will highlight their most important concepts and I will show techniques, how-to and best practices. It is obvious that it would take a series of books to cover everything about macOS programming, not just a series of tutorials.

Whenever possible, we will be going through the making process of a new macOS app that will have some usefulness at the end. Something that you can take with you when finish reading each post. So, once again welcome to a new exciting journey!

In this post we are going to see the basics of a macOS app through the creation of a simple, but at the same time useful for many developers app: A font viewer that will let us choose font families and it will be displaying the PostScript names of the available fonts.

There are even more things to discuss in this post in addition to those listed above. We will talk about new classes that will be proved extremely valuable for our cause, we will talk about the custom logic that we will implement in the various parts of the app, and we will see small or big details that will produce at the end a complete project. Set that as the value to the Product Name field, and configure the other properties such as Team, Organization Name, Organization Identifier.

Generally, make sure that your window looks like the next one:.

macOS Development for Beginners: Part 1

Look and play around with it. You will notice that standard behaviour and attributes are assigned by default to the window, such as resizing, moving or going full screen. If you have prior experience to iOS development, you will find out that a macOS project looks pretty much the same to an iOS project.

Project Navigator lists all files and groups, tabs like General, Capabilities, Build Settings, etc are here too, and generally everything looks familiar. Displayed settings and options are specific to macOS though.

In the Project Navigator you will find all files and groups that Xcode creates by default for a Cocoa App. What is new here comparing again to iOS projectsis an.The original tutorial was written by Mikael Konutgan.

Menu bar apps have been staple of macOS for a long time. Many apps like 1Password and Day One have companion menu bar apps. Fire up Xcode. Then make sure that Swift is selected as the language, and that Use Storyboards is checked. Finally, click Next again, choose a place to save the project and click Create. Once the new project is set up, open AppDelegate. This creates a Status Item — aka application icon — in the menu bar with a fixed length that the user will see and use.

Go to Assets. Select the image and open the attributes inspector. Change the Render As option to Template Image. If you use your own custom image, make sure that the image is black and white and configured as a template image so the Status Item looks great against both light and dark menu bars.

Back in AppDelegate. This will configure the status item with an icon of the image you just added, and an action for when you click on the item. Take note of the objc directive in the signature. This exposes the method to the Objective-C runtime to allow the button to use it as an action.

Note : If you have too many menu bar apps, you might not be able to see your button. Switch to an app with fewer menus than Xcode like Finder and you should be able to see it.

macos menu bar app swift

To disable the dock icon, open Info. Build and run. High five yourself :]. Usually, a measly single action on click is not enough for a menu bar app. The easiest way to add more functionality to your app is to add a menu.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here.

Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Important: Using a custom view requires to implement all methods to handle the highlighting, mouse clicks and NSMenu delegate.

Learn more. How to show two row of text in a menu bar app in mac os Ask Question. Asked 3 years, 4 months ago. Active 3 years, 4 months ago.

Viewed times. See the following topic. It's for Objective-C. But you will get some idea. Active Oldest Votes. This kind of app is called menu bar app or menulet. Draw the text in drawRect Important: Using a custom view requires to implement all methods to handle the highlighting, mouse clicks and NSMenu delegate.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Socializing with co-workers while social distancing. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response….

NSTouchBar Swift Tutorial – Integrate Touch Bar in macOS Apps

Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow. Triage needs to be fixed urgently, and users need to be notified upon…. Dark Mode Beta - help us root out low-contrast and un-converted bits. Linked Related 4. Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.Good news! But this is not a requirement until you are ready to send your app out to the world, and even then, only if you want to distribute through the Mac App Store.

If you already have a developer account for distributing iOS apps, then you are all set — Apple has merged the developer accounts so that you only need a single account to distribute apps for any Apple devices. Unlike some other platforms, developing for macOS requires the installation of just one tool: Xcode. You will need an App Store account to download Xcode even though Xcode is free.

Search for Xcode and click the Install button to start the download. Once it has downloaded and installed which may take a while — it is quite large open it from your Applications folder.

Subscribe to RSS

The first time you run Xcode, and after every major update, it will ask you for permission to install additional components. Enter your password and allow Xcode to install these components. Following the long-standing tradition when learning a new programming language or platform, you are going to start by creating a Hello World! Open Xcode if it is not already running. Click Create a new Xcode project and when the next dialog appears, choose macOS from the tabs across the top.

Select Cocoa Application from inside the Application section and click Next. Give your new app a name — HelloWorld — make sure that the language is set to Swift and that Use Storyboards is checked.

Uncheck all the other options. Xcode has created the basic template for your app with all the required files. At this stage, it is fun to run the app and see how much you get for free. Click the Play button in the toolbar to run the app or use the Command-R shortcut. Xcode will now compile all of the code into machine code, bundle up the resources required by the app and then execute it. Note: The first time you ever build and run an app in Xcode, you might be asked whether you want to Enable Developer Mode on this Mac.

macos menu bar app swift

Developer mode allows Xcode to attach a debugger to running processes — which will be extremely useful when building your application!

Xcode packs a lot of features into a small package, so not everything is visible at one time. The left panel is the Navigator panel and has 8 display options across the top. The one you will mostly use is the first one — Project — which lists all the files in your project and allows you to click on any one to edit it.

The center panel is the Editor panel and will display whatever you have selected from the Project Navigator. The right panel is the Utilities panel and it will vary depending on what you are looking at in the Editor panel.

You design the user interface using a Storyboard. Your app already has a storyboard, so go to the Project Navigator and click on Main. Your display has just changed dramatically! Have a look at the things you can see in the visual editor. There are three main areas, each of which also has a textual representation in the Document Outline:. In the Utilities panel, you see a top section with 8 tabs and a bottom section with 4 tabs. The bottom section switches between various things you can insert into your project.

Right now you want to insert UI elements, so select the Object library which is the third from the left.Good news! But this is not a requirement until you are ready to send your app out to the world, and even then, only if you want to distribute through the Mac App Store. If you already have a developer account for distributing iOS apps, then you are all set — Apple has merged the developer accounts so that you only need a single account to distribute apps for any Apple devices.

Unlike some other platforms, developing for macOS requires the installation of just one tool: Xcode. You will need an App Store account to download Xcode even though Xcode is free. Search for Xcode and click the Install button to start the download.

Once it has downloaded and installed which may take a while — it is quite large open it from your Applications folder. The first time you run Xcode, and after every major update, it will ask you for permission to install additional components.

Enter your password and allow Xcode to install these components. Following the long-standing tradition when learning a new programming language or platform, you are going to start by creating a Hello World! Open Xcode if it is not already running. Click Create a new Xcode project and when the next dialog appears, choose macOS from the tabs across the top. Select Cocoa Application from inside the Application section and click Next. Give your new app a name — HelloWorld — make sure that the language is set to Swift and that Use Storyboards is checked.

Uncheck all the other options. Xcode has created the basic template for your app with all the required files. At this stage, it is fun to run the app and see how much you get for free. Click the Play button in the toolbar to run the app or use the Command-R shortcut. Xcode will now compile all of the code into machine code, bundle up the resources required by the app and then execute it. Note: The first time you ever build and run an app in Xcode, you might be asked whether you want to Enable Developer Mode on this Mac.

Developer mode allows Xcode to attach a debugger to running processes — which will be extremely useful when building your application! Xcode packs a lot of features into a small package, so not everything is visible at one time.

The left panel is the Navigator panel and has 8 display options across the top. The one you will mostly use is the first one — Project — which lists all the files in your project and allows you to click on any one to edit it. The center panel is the Editor panel and will display whatever you have selected from the Project Navigator. The right panel is the Utilities panel and it will vary depending on what you are looking at in the Editor panel. You design the user interface using a Storyboard.

Your app already has a storyboard, so go to the Project Navigator and click on Main. Your display has just changed dramatically! Have a look at the things you can see in the visual editor. There are three main areas, each of which also has a textual representation in the Document Outline:.

In the Utilities panel, you see a top section with 8 tabs and a bottom section with 4 tabs.A contextual menu, or shortcut menugives people access to frequently used commands related to the current context. A contextual menu is revealed by Control-clicking a view or selected element in an app. For example, Control-clicking selected text in TextEdit displays a contextual menu containing text-specific menu items for initiating actions like changing the font and checking spelling.

Always follow menu design best practices. In general, all menus and menu items should be consistently arranged and titled. See Menu Anatomy. Include only the most commonly used commands that are appropriate in the current context. Limit the hierarchical depth of contextual menus to one or two levels. Submenus in contextual menus can be difficult to navigate without accidentally dismissing the contextual menu. If you must include submenus, restrict them to a single level.

If the user opens the menu and closes it without selecting anything, no action should occur. Always make contextual menu items available in the menu bar too. A contextual menu is hidden by default and a user might not know it exists, so it should never be the only way to access a command. In particular, avoid using a contextual menu as the only way to access an advanced feature. See Menu Bar Menus.

macos menu bar app swift

Show keyboard shortcuts in menu bar menus, not contextual menus. Contextual menus are already shortcuts to task-specific commands; it's redundant to display keyboard shortcuts too. Use an Action pop-up button to elevate contextual menu functionality. You can use an Action pop-up button to provide app-wide contextual menu functionality in a toolbar. See Action Pop-Up Buttons. Contextual Menus A contextual menu, or shortcut menugives people access to frequently used commands related to the current context.


Join the Conversation

Comments

Leave a comment

Your email address will not be published. Required fields are marked *