Skip to content

Instantly share code, notes, and snippets.

@pookjw
Last active March 27, 2025 05:24
Show Gist options
  • Save pookjw/9f0200f59f7f830794063d68f2fffc00 to your computer and use it in GitHub Desktop.
Save pookjw/9f0200f59f7f830794063d68f2fffc00 to your computer and use it in GitHub Desktop.
import SwiftUI
import ObjectiveC
fileprivate var oldIMP: IMP!
fileprivate func swizzle() {
let ToolbarPlatformDelegate: AnyClass = objc_lookUpClass("_TtC7SwiftUI23ToolbarPlatformDelegate")!
let method = class_getInstanceMethod(ToolbarPlatformDelegate, #selector(NSToolbarDelegate.toolbar(_:itemForItemIdentifier:willBeInsertedIntoToolbar:)))!
oldIMP = method_getImplementation(method)
let newIMPFunc: @convention(c) (AnyObject, Selector, NSToolbar, NSToolbarItem.Identifier, ObjCBool) -> NSToolbarItem? = { `self`, _cmd, toolbar, itemIdentifier, flag in
let oldIMPFunc = unsafeBitCast(oldIMP, to: (@convention(c) (AnyObject, Selector, NSToolbar, NSToolbarItem.Identifier, ObjCBool) -> NSToolbarItem?).self)
let item = oldIMPFunc(self, _cmd, toolbar, itemIdentifier, flag)
if let item, itemIdentifier.rawValue == "com.apple.SwiftUI.navigationStack.back" {
// SwiftUI.SwiftUISegmentedControl
let control = item.view!.subviews.first!.subviews.first! as! NSSegmentedControl
control.sizeToFit()
item.view!.setFrameSize(control.bounds.size)
}
return item
}
method_setImplementation(method, unsafeBitCast(newIMPFunc, to: IMP.self))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment