Freelance iOS Developer, Rapidly Aging Punk

Getting NSToolbar Height From an NSWindow in Swift

Jan 15, 2015 at 01:58PM

Adapted from this old Apple example:

import Cocoa

extension NSWindow {

    func toolbarHeight() -> CGFloat {
        var toolbar: NSToolbar?
        var toolbarHeight = CGFloat(0.0)
        var windowFrame: NSRect

        toolbar = self.toolbar

        if let toolbar = toolbar {
            if toolbar.visible {
                windowFrame = NSWindow.contentRectForFrameRect(self.frame, styleMask: self.styleMask)
                toolbarHeight = NSHeight(windowFrame) - NSHeight(self.contentView.frame)

        return toolbarHeight

If you find that this code is returning 0 but you're positive you have an NSToolbar, check that Full Size Content View isn't selected in the attributes inspector of your storyboard.