Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Forked from brentsimmons/NSMenuItem+RSCore.swift
Last active January 11, 2026 13:45
Show Gist options
  • Select an option

  • Save jonsterling/500feb6df06d854a9b1edfa30fcb31a9 to your computer and use it in GitHub Desktop.

Select an option

Save jonsterling/500feb6df06d854a9b1edfa30fcb31a9 to your computer and use it in GitHub Desktop.
Disable all menu item images in a Mac app.
//
// NSMenuItem+RSCore.swift
// RSCore
//
// Created by Brent Simmons on 1/5/26.
//
#if os(macOS)
import AppKit
import ObjectiveC
extension NSMenuItem {
/// Disables all icons in all regular menu items.
///
/// Call `NSMenuItem.disableIcons` early (from AppDelegate.init is good).
public static func disableIcons() {
let originalSelector = #selector(getter: image)
let nilImageSelector = #selector(returnNilInsteadOfImage)
guard
let originalMethod = class_getInstanceMethod(NSMenuItem.self, originalSelector),
let newMethod = class_getInstanceMethod(NSMenuItem.self, nilImageSelector)
else {
return
}
method_exchangeImplementations(originalMethod, newMethod)
}
@objc private func returnNilInsteadOfImage() -> NSImage? {
// In a palette menu, as appears for example in the "zoom" button of a window,
// we want the icons to appear because labels are hidden.
if let menu, case .palette = menu.presentationStyle {
// Because the method implementations are swapped, this will return the original image.
return self.returnNilInsteadOfImage()
} else {
return nil
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment