Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pookjw/e8c60939763f48e0265abc6e2b05aeab to your computer and use it in GitHub Desktop.
Save pookjw/e8c60939763f48e0265abc6e2b05aeab to your computer and use it in GitHub Desktop.
#import "ViewController.h"
#import <objc/message.h>
#import <objc/runtime.h>
NSNotificationCenter * __viewGeometryNotificationCenter;
id (*nc_originalInit)(id, SEL);
id nc_customInit(id self, SEL _cmd) {
__viewGeometryNotificationCenter = self;
return nc_originalInit(self, _cmd);
}
@interface ViewController ()
@property (weak) IBOutlet NSButton *upButton;
@end
@implementation ViewController
+ (void)load {
Method method = class_getInstanceMethod([NSNotificationCenter class], @selector(init));
nc_originalInit = (typeof(nc_originalInit))method_getImplementation(method);
method_setImplementation(method, (IMP)nc_customInit);
[[NSTrackingSeparatorToolbarItem alloc] initWithItemIdentifier:@""];
method_setImplementation(method, (IMP)nc_originalInit);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSButton *upButton = self.upButton;
((void (*)(id, SEL))objc_msgSend)(upButton, sel_registerName("enableGeometryInWindowDidChangeNotification"));
[__viewGeometryNotificationCenter addObserver:self selector:@selector(foo:) name:@"NSViewGeometryInWindowDidChangeNotification" object:upButton];
}
- (IBAction)up:(NSButton *)sender {
NSRect oldRect = sender.frame;
sender.frame = NSMakeRect(oldRect.origin.x, oldRect.origin.y + 1., oldRect.size.width, oldRect.size.height);
}
- (void)foo:(NSNotification *)notification {
NSLog(@"%@", notification);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment