Apply constraints on NSMenuItem that has been replaced by custom view

load nsview from xib swift
custom nsview swift
custom uiview with xib objective-c
cocoa custom nsview
custom uiview with xib swift 4
macos storyboard tutorial
init nsview from xib
nsmenu tutorial

I have created a main menu in Swift/Cocoa with custom views that replaces several menu items. The custom views are loaded from a separate NSView class/xib File. The constraints inside this custom view hold a label that is centered via Interface Builder, accordingly:

The replacement of the menu item has been accomplished like this:

@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var tabviewRoom: tabviewMenuRoom!
@IBOutlet weak var stackScenes: NSStackView!
@IBOutlet weak var menuRoomsHeader: viewMenuHeaderRooms!


if let itemRoomsHeader = statusMenu.item(withIdentifier: "rooms_header") {
        menuRoomsHeader.title = "Rooms"
        itemRoomsHeader.view = menuRoomsHeader = self

However, due to some missing constraints onto the view, the final result in the menu looks like this (the NSView is not stretched to the width of the menu item):

I am wondering how to add constraints to a menu item to keep the text centered (=width of the custom NSView shown in red, width of NSMenuItem shown in green).

Make sure you have disabled "Translates Mask Into Constraints" for the container view. Then check that the layout behaves as intended when you resize the view in Interface Builder.

You can also review active constraints at runtime by opening the menu, then clicking "Debug View Hierarchy" in Xcode and selecting NSCarbonMenuWindow in the Debug Navigator.

Swift Development with Cocoa: Developing for the Mac and iOS App , Make sure you have disabled “Translates Mask Into Constraints” for the container view. Then check that the layout behaves as intended when you resize the  I'm using cusom views in the NSMenuItem of my menu bar application. On small resolutions though there is a scrolling glitch. (see video) Please notice the sliders overlapping the text and also stacking on the bottom.

It looks like your only intention is to bring the menu title in the middle and to make it bigger in size. If so, the way to go is with menu item attributedTitle.

@IBOutlet weak var menuHeader: NSMenuItem!

func setMenuTitle() {

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center

    let textAttributes = [NSAttributedString.Key.font: NSFont.systemFont(ofSize: 32),
                          NSAttributedString.Key.paragraphStyle: paragraphStyle]

    let attString = NSAttributedString(string: "Title", attributes: textAttributes)

    menuHeader.attributedTitle = attString


If this is not working for you and you need more customisation take a look what Apple is writing on sizing a view in a NSMenuItem:

A menu item with a view sizes itself according to the view's frame, and the width of the other menu items. The menu item will always be at least as wide as its view, but it may be wider. If you want your view to auto-expand to fill the menu item, then make sure that its autoresizing mask has NSViewWidthSizable set; in that case, the view's width at the time setView: is called will be treated as the minimum width for the view. A menu will resize itself as its containing views change frame size. Changes to the view's frame during tracking are reflected immediately in the menu.

Cocoa Design Patterns: Cocoa Design Patterns _1, prefPane fileonto the System Preferences application inthe Dock. Status items candisplay anytext, image,or view when clicked, and can display either a menu ora custom view. or view, and providing it with an NSMenu or other view to display whenit's clicked. The user interface for the application, includingits constraints. A menu item with a view does not draw its title, state, font, or other standard drawing attributes, and assigns drawing responsibility entirely to the view. Keyboard equivalents and type-select continue to use the key equivalent and title as normal.

From Views in Menu Items:

If you want your view to auto-expand to fill the menu item, then make sure that its autoresizing mask has NSViewWidthSizable set.

This is the horizontal line in the middle of the Autoresizing control.



macOS Programming Tutorial: Working with Custom Views and , Whether Core Data is used, the design of persistent information storage If the members of a family are represented by objects, each member optionally has a relationship to are stored so that you can use custom objects or C structures as attributes. Figure 1.3 Cocoa's principal View subsystem classes The NSMenu,​  If you have a NSMenuItem with a custom view (using NSMenuItem#view) in a NSMenu that's attached to a NSStatusItem, the NSMenu will have a top shadow that overlaps the status bar, instead of the status bar having a shadow overlapping the NSMenu.

AppKit Release Notes for macOS 10.14, The above two views are custom views that they will be eventually loaded so you don't have to apply constraints manually while we'll be adding Cocoa you can change, so the default look of the label is changed. to a view, where an item is actually a menu item ( NSMenuItem ), another Cocoa control. It is actually possible to have your custom view flash like a regular NSMenuItem without implementing the animation manually. Note: this uses a private API and also fixes a handful of other strange NSMenuItem quirks related to custom views. NSMenuItem.h. #import <AppKit/AppKit.h> @interface NSMenuItem () - (BOOL)_viewHandlesEvents; @end

view, The fillColor of a custom NSBox is set to controlBackgroundColor Omitting a minimum width or height constraint will use the view's intrinsic content size, type is imported in Swift, see Grouping Related Objective-C Constants. NSMenu The default typesetter behavior has changed for apps that are compiled with the  I thought I'd be clever and use a 'dummy' NSMenuItem with a `nil` action and no key equivalent as a title heading in an NSMenu. I have a template NSMenu in IB which I'm populating programmatically with this code:

Custom nsview swift, the key equivalent and title as normal. For more details, see Application Menu and Pop-up List Programming Topics. By default, a menu item has a nil view. However, that if body is the best place to perform actions after the custom view has been loaded. The PersonalInfoView class can now load its custom view from the Xib file, so let’s do the exact same things for the WorkInfoView class. Open the WorkInfoView.swift file and first of all adopt the LoadableView protocol: