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
itemRoomsHeader.target = 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).
menuRoomsHeader
is, and how it's related toitemRoomsHeader
? – iOShit I Screwed UptabviewMenuRoom
is of typeNSTabView
andviewMenuHeaderRooms
is of typeNSView
. The classes consists of their inits only, no programmatic layouting etc. The whole formatting is done in Interface Builder. – Maschina