Nstoolbarflexiblespaceitem Is Constraint to Nssplitviewitem in Swift

Display Only Customize Toolbar... in NSToolbar's Context Menu in Swift

After 3 days, I finally did it. Here is the result.

Sample Image

Sample Image

Source Code in Swift 3

You can implement and make your own class, but here I just want to keep everything in a file.

This is the WindowController.swift file. You can set the custom class of your window controller and run. Again thanks to @1024jp for the tips.

//
// WindowController.swift
// The Toolbar
//
// Created by João Oliveira on 22/09/2016.
// Copyright © 2016 João Oliveira. All rights reserved.
//

import Cocoa

class WindowController: NSWindowController {

override func windowDidLoad() {
super.windowDidLoad()

guard let window = window else { return }

window.delegate = self

window.toolbar = NSToolbar(identifier: "RestrictedToolbar")
window.toolbar?.allowsUserCustomization = true
window.toolbar?.displayMode = .iconOnly
window.toolbar?.delegate = self

keepOnlyCustomizableMenu()
}

// PROBLEM 1: Solution
func keepOnlyCustomizableMenu() {
if let contextMenu = window?.contentView?.superview?.menu {
contextMenu.items.forEach({ (item) in
if let action = item.action,
NSStringFromSelector(action) != "runToolbarCustomizationPalette:" {
contextMenu.removeItem(item)
}
})
}
}
}

// MARK: Window Delegate
// A ton of thanks to genius @1024jp
extension MyWindowController: NSWindowDelegate {

// PROBLEM 2: Solution
func window(_ window: NSWindow, willPositionSheet sheet: NSWindow, using rect: NSRect) -> NSRect {

if sheet.className == "NSToolbarConfigPanel" {
removeSizeAndDisplayMode(in: sheet)
}

return rect
}

func removeSizeAndDisplayMode(in sheet: NSWindow) {

guard let views = sheet.contentView?.subviews else { return }

// Hide Small Size Option
views.lazy
.flatMap { $0 as? NSButton }
.filter { button -> Bool in
guard let buttonTypeValue = button.cell?.value(forKey: "buttonType") as? UInt,
let buttonType = NSButtonType(rawValue: buttonTypeValue)
else { return false }
return buttonType == .switch
}
.first?.isHidden = true

// Hide Display Mode Option
views.lazy
.filter { view -> Bool in
return view.subviews.count == 2
}
.first?.isHidden = true

sheet.contentView?.needsDisplay = true
}

}

// MARK: Toolbar Delegate
extension MyWindowController: NSToolbarDelegate {

func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [String] {
return [
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarSpaceItemIdentifier,
NSToolbarToggleSidebarItemIdentifier
]
}

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [String] {
return [NSToolbarToggleSidebarItemIdentifier]
}

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: String, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
return nil
}

}

NSToolbar Flexible space not working in Swift 5.x

Had the same problem with Swift 5.0 ... even though the documentation says minSize and maxSize are deprecated, I solved this like:

let toolbarItem = NSToolbarItem(itemIdentifier: NSToolbarItem.Identifier.flexibleSpace)
toolbarItem.minSize = NSSize(width: 1, height: 1)
toolbarItem.maxSize = NSSize(width: 1000, height: 1) //just some large value
return toolbarItem

Maybe this works as well for Swift 5.2



Related Topics



Leave a reply



Submit