Subclassing an NSDrawer's contentView to implement a completely custom drawer - objective-c

I want to implement my own custom drawer completely by subclassing NSView and doing all my view drawing in that. I've created a custom NSView class that does hardly anything apart from implement initWithFrame: and drawRect: which I've got logging the frame/bounds of the NSView (which is reporting correctly). I've also instantiated this view and added it to the NSDrawer object in my application using setContentView: so that it uses my custom NSView.
However, this still draws a default drawer layout attached to the edge of my applications NSWindow. How do I override this default style so that I can draw my own drawer (!) in my custom, subclassed NSView without anything default being drawn by the OS? (So that I can control the design and size of the drawer myself, to basically emulate a tab bar that won't act strictly as a traditional drawer.)

I made a custom drawer by subclassing NSWindow rather than NSDrawer. It was a lot of work. There is a private object, NSThemeFrame, that sits between a NSWindow and its contentView. To avoid using private API, you have to make a transparent window and let its contentView act like a NSThemeFrame. Matt Gallagher shows how: http://cocoawithlove.com/2008/12/drawing-custom-window-on-mac-os-x.html

That frame is being drawn by the drawer's frame view. To do this, you need to use some private methods, and the easiest way is probably to use a custom subclass of NSDrawer. I do not know the specific methods used for drawers, but you can get a header for the class including private methods using class-dump.
Edit: This is what NSDrawer actually does.
NSDrawer is an opaque object that manages other objects. It creates a window using the private NSDrawerWindow class. Setting its content view sets the window's content view. The NSDrawerWindow class uses the private NSDrawerFrame class, which is a subclass of the private NSFrameView class, as its background and displays its content view inside that.
To change the frame, you need a way to change the frame view in the window. The easiest thing to do would be to get a header for NSDrawerWindow and add a category that overrides +frameViewClassForStyleMask: to return the class for your custom view. Your custom view should be a subclass of NSFrameView, which means you also need a header for that class.

Related

Accessing class functions

I have created a UIView class. In this class it has touchesbegan and touches moved functions etc. When I create an instance of this class in my UIView controller they don't work.
Is it not possible for the instance to access these functions? I know the class works because if I drag a UIView onto the View Controller and assign it with the class I can drag it and resize it etc.
I have created a UIView class. In this class it has touchesbegan and
touches moved functions etc. When I create an instance of this class
in my UIView controller they don't work.
Check the userInteractionEnabled property of the view. The default value for any UIView is YES, so your view should track touches, but you may have disabled the view in your storyboard or .xib file.
Is it not possible for the instance to access these functions?
It's possible, and the entire touch handling mechanism relies on it. If your view is enabled, and it's not covered up by some other view, and there aren't any gesture recognizers that are stealing its touch events, it should be receiving touch messages.

Transparent NSMenu

I'm trying to change the opacity of a NSMenu, I made custom NSMenuItems and changed their opacity but there is still the white color of menu behinde them, I'm looking for a way too change the color and opacity of the menu background. I was wondering if it's even possible?
Regards
I don't think there is an elegant way to do that. NSMenu isn't a subclass of NSView. The rendering view is probably a private variable of the menu object.
NSMenuItem has a view property that you can use. Try setting a custom view (if you don't already) and later find the menu's view by calling menuItem.view.superview. Then set that view's alpha property.
Alternatively you could try to find the private view property of NSMenu with a tool like class-dump. Note that using private variables is never a good idea. Especially if you're planning to release the app on the App Store.

In Interface Builder, how can I add UIViews to a subview that is a part of a custom view I've created?

I am working to create a custom view for an iPhone app I'm creating. This custom view is a Popover dialog which is made up of a UIView which contains two images, a button to close the dialog, a label, and a UIScrollView. This view is named MDPopoverCard. I have these files as a part of my view:
MDPopoverCard.xib - The view as drawn up in Interface Builder.
MDPopoverCard.h - Defines a few IBActions and some other properties
MDPopoverCard.m - Implements some functions defined in the header
This is what it looks like in Interface Builder: http://cl.ly/2B0f2x3s1w1i0K2G0Q1r (sorry, I can't post an image yet as I'm new to stackoverflow)
There are a few properties defined in my .m and .h files that control whether the green button is displayed and what the text of the title label is.
I need to display a number of these dialogs in my app and I'd like to reuse this interface I've designed. I want to be able to add buttons and other form elements into the UIScrollView via Interface Builder. However, I have a problem:
Imagine that I have another view I'm drawing up in Interface Builder. I add a UIView to it and set its class to MDPopoverCard. I then drag a couple UIButton objects into my MDPopoverCard view. Here's an example of what it looks like in Interface Builder:
http://cl.ly/1X090h1t1q3f0i3E0917
This screenshot shows another view (the root view) that I've added my MDPopoverCard to. I've then added two buttons as subviews of MDPopoverCard.
These buttons do get properly nested in Interface Builder. However, when I run my app these buttons are added before any of the items that make up my MDPopoverCard view in the xib file. This means that the buttons are being added behind my popover dialog. That's the first problem.
The second problem is that I want these buttons and form elements to actually be added into the UIScrollView that's contained within the MDPopoverCard view, and not just right into the UIView's subviews array. Is there a way to specify this in Interface Builder? I'd really much rather draw buttons into my UIView and connect them to IBActions via Interface Builder than hand write every instance of these dialogs that I may need to display (several).
Any advice? Is there anything I can do to clarify the question?
Thanks for your help!
Formerly Xcode supported user-defined IB plugins for custom UI elements which you could just drag and drop into the XIBs the same way you do with built-in widgets. As of Xcode 4 this nice feature has been removed. (Thanks a lot, Apple.)
Currently I can only think of a hacky way to achieve what you described. What I would do is the following:
create an IBOutletCollection on your MDPopoverCard, e.g. embeddedControls
link it with every UI element (here: the buttons) you want to go inside the scroll view
implement the awakeFromNib in MDPopoverCard and explicitly reset the superview of all the views in embeddedControls to the scroll view in there
Hope this helps (although I haven't tried).

NSScrollView disable autoscrolling

How do I disable the autoscroll in a NSScrollView as I'm filling it with content?
I have subclassed it in my own class but I don't find an appropriate method to override.
There's probably more than one way to go about this, but take a look at -[NSClipView constrainScrollPoint:]. It's not meant to be called directly, rather to be overridden in an NSClipView subclass. NSClipView is the class that actually does the real work of scrolling the content of an NSScrollView.
You should be able to override it to simply return the document view's current origin point when you don't want scrolling to happen. It's worth noting that this will also disable scrolling via the user-visible interface, so you should only do it while you're filling the view with content. Otherwise, return the result of a call to the superclass's implementation.

Should I use IB or Subclass UIView

So, I developed a kind of drop down button class.
Let's call it DDButton.
I mainly export one function :
-(void) addButtonWithImage:(UIImage*)image andTarget:(id)target andSelector:(SEL)selector
which lets the user add another button to the drop down.
I will need to use DDButton in different screens of my app.
I would like to use it like:
DDButton* ddb = [[DDButton alloc] initWithFrame:rect];
[ddb addButtonWithImage....]
[ddb addButtonWithImage....]
My question is since I never subclassed UIView before how should I implement it, and how should I use it later ?
Do I use IB and create a stub UIView which I'll connect to the DDButton in the Identity Pane ?
if so , how exactly I instantiate the view later on.
Or,
Do I subclass UIView ? if so , what methods I should override ? Do you I setup my buttons in the initializer ? in LayoutSubView ? In drawRect ?
I would love to hear the best approach here.
Thanks!
Edit
Let's say I choose the IB way : I have a main button which I set regardless of the
addButtonWithImage() calls, actually all calls to addButtonWithImage just "append" to that button. I want to main button to be the size of the view, until other buttons are added and then the view grows appropriately. However, I want the size of the view to be chosen by the user at first...using setFrame I guess.
Meaning in the awakeFromNib I can't count on the frame size yet (it only take the xib size I assume). So where would I setup my main button ? LayoutSubView ? setFrame ? I'm not sure.
Add your view to the interface in IB as a UIView, then change the class in the identity pane. If you need to do initialization in code, use a -(void)awakeFromNib method. I would suggest setting up the buttons when they are added in addButtonWithImage....
I'd probably do a subclass, building views in code is a good thing to learn.
Override drawrect: to do any custom drawing you need to do, if you're just adding a UIImageview or something and doing positioning you could just override initWith...: and do your custom initialisations.