Subclass NSMenu - macos

This is a simple question...
Just: How would I go about subclassing NSMenu? As I'm not sure about how to go about doing it.
I'd like to subclass it in order to do custom drawing eg. change background colour etc.
This is normally done with - (void)drawRect:(NSRect)dirtyRect thus I tried subclassing and using that method, to no avail.

You can't do this by subclassing NSMenu. You want to use NSMenuItem's setView: to do custom drawing in a menu. Take a look at the MenuItemView sample code for an example of how it's done and what you can do with it.

Related

registerForDraggedTypes with NSStatusBarButton?

I'm trying to implement file drag and drop with an NSStatusBarItem.
All I really have is statusBarItem.button!.window?.registerForDraggedTypes([NSFilenamesPboardType]), but I don't know how or where to actually override methods like draggingEntered and performDragOperation.
I know it can be done with a custom view, but I'd like to stay away from deprecated methods if possible.
NSButton inherits from NSView. Therefore you can overlay a custom view which implements drag and drop functionality and add it as a subview to the NSButton. This approach avoids deprecated methods.

Cocoa Programming: Adding a Rectangle to a Custom View (NSView)

Is there a simple way to add a simple rectangle to a Custom View without using a custom NSView subclass for it? Something along the lines of:
Assign an IBOutlet (let's call it colorWheelView) of NSView type to the CustomView
In my NSViewController's initWithNibName use it to change draw the rectangle:
// pseudocode
self.colorWheelView.addRectangle(myRectangle);
self.redraw()
The only way I've seen it done (on this site, and in my book Cocoa Programming for Mac OSX, pp. 241) is by making a custom class for the Custom View and modifying its drawRect method... Is this really the only way to accomplish this?
Edit: not sure why formatting is not being rendered correctly. I'm trying to fix it.
It really isn't all that hard to roll your own..
Just add an NSArray property to your NSView subclass, then in your drawRect method draw them either manually or using one of the NSRectFillList* methods provided by AppKit already.
(Beware: those take a plain C array, not an NSArray).
You wouldn't want to manually trigger the redraw from outside the view as in your sample code, though. To keep things consistent your addRectangle would trigger a redraw of the view itself e.g. by calling setNeedsDisplay:.

How to tell what's causing drawRect to be called?

I've got my custom NSView with a bunch of custom buttons in it, the buttons are added as a subView in the NSView's drawRect method.
Now I'm finding that after pressing a button the drawRect of the parent view is repeatedly called. Sometimes it only stops when I quit the app - I know this from a simple log statement in drawRect.
Now I know there are probably bigger architectural issues in my app causing this, where do I go to begin tracking down what's causing this view to be repeatedly redrawn?
Thanks!
First of all you shouldn't be adding subviews in drawRect:.
Are you doing any custom drawing or are you just adding subviews? If you're not doing any drawing, you should not implement drawRect:.
You want to add the subviews in initWithFrame: and then you want to set the frames of the subviews in layoutSubviews based on self.bounds.
If you have any questions, feel free to ask.
EDIT: Just realized that you were asking about NSView and not UIView. I've never used NSView, but perhaps they work similarly.
EDIT 2: I read a bit about NSView and it doesn't seem to have layoutSubviews. Perhaps you should set the frames in drawRect:? I'm still pretty sure you don't want to add subviews in drawRect:.

buttons in interface buider

has anyone an idea if it is possible to create ellipse or polygon buttons in IB?
Directly, not without a library.
You can, however, create an NSView subclass which performs your shape drawing for you. You will need to implement the mouse events to perform clicks correctly. Drop an NSView on your window, set its class in the inspector to your custom-drawn NSView subclass, and you're set. Check out this tutorial.
You could also create a custom control, derived from NSControl. Check out this documentation.

How do you create a custom themed NSButton?

How do you create a custom themed NSButton? I don't mean in a small way like changing the background color or changing from rounded edges to square edges. I want to replace the entire look and feel of the button. Is that even possible to do in Cocoa? Obviously I would have to subclass the NSButton class and go from there. Any help would be much appreciated.
Actually, you need to subclass NSButtonCell. You should read Apple's documentation on this to gain a better understanding of how they interact. You probably will still want to subclass NSButton so that it will use your NSButtonCell subclass, too.
For a button, most of the work is done in drawBezelWithFrame:inView:. If you want to alter the way the text or image is drawn, you would override drawTitle:withFrame:inView: and drawImage:withFrame:inView:.

Resources