Auto expand an NSWindow to fit an NSToolbar using auto layout - macos

I have an NSWindow with an NSToolbar that I present as a sheet. I want the NSWindow to resize to fit the NSToolbar (so the buttons don't get chopped.)
The toolbar is not user configurable, but its size will change due to localisation.
I'm trying to achieve this using auto layout.
Any pointers would be most welcome.
Thanks

Related

If a NSView uses autolayout, do all of its subviews also need to use autolayout for positioning?

I have a view in a window, the position and size of the view are calculated with autolayout. The view has a subview, a draggable NSView subclass. It is really easy to make a NSView "draggable" by overriding -mouseDown: and -mouseDragged: and changing the frame of the view directly.
The view hierarchy is as follows,
What is the best way of making the subview draggable in this case?
For example,
Is it possible for the subview to not use autolayout, so that it can be positioned by changing the frame directly? i.e. the window positions the main view, but then autolayout does not layout the subview inside the main view. Or do all views in the hierarchy need to use autolayout?
When I have used autolayout before, I have used it to make "fixed" layout that respond to resizing. But dragging a view with a mouse does't seems like a natural use-case for autolayout.
Is it possible for the subview to not use autolayout, so that it can be positioned by changing the frame directly?
Yes. If you keep translatesAutoresizingMaskIntoConstraints turned on, it automatically creates constraints from the values of frame and autoresizingMask.
In fact, this means it will use Auto Layout, but you can work with frame just like with manual positioning.
The best way is to not avoid auto layout.
Making a view draggable is pretty easy with auto layout constraint IBOutlets and getting the mouse delta from NSEvent short circuit mouse tracking.

Highlight NSImageView in NSView

I have a simple question. I have my NSView which is detecting drops (drag and drop). When user drops a link with image from browser, I detect that action, create NSImageView, initialize it on a place where user dropped it with some default frameSize and put the image from the link into it.
I would now like to highlight that NSImageView when user clicks on it. I also want to implement moving around that NSImageView in NSView but I'm pretty sure I will manage that. How do I highlight that exact NSImageView which was clicked? I haven't created earlier that NSImageView in interfacebuilder and assigned a special class for it so I can use drawRect, I have just created it dynamically...
Any help would be appreciated.
You can use NSImageView's setImageFrameStyle for highlighting.

Hide NSView in StatusBar Application

I have a small NSView (HUD-Style) in my StatusBar-Application.
If I get it right this NSView does not have a superview.
My problem is that I only want to show this view under certain programmatic circumstances.
What is the best way to show or hide this view?
One way to do this is to show this view in a borderless transparent window (NSBorderlessWindowMask).

Creating NSToolbar style buttons

Is there a bevel style that can replicate NSToolbar style of buttons that are used, for instance, in the Safari's preferences window to switch between different panes?
I need to replicate NSToolbar in an NSView using NSButtons. I understand that I should probably be using NSTabView, but I'd like to implement the look of xcode's left pane. Any tips here would be appreciated greatly.
There's nothing built-in, you'd have to create the images yourself. However, replicating the behaviour is straightforward.
You could simply use a single-row NSMatrix of NSButton objects. Just give the buttons an image and an alternate image (for the highlighted state) and set the matrix mode to NSRadioModeMatrix.

NSToolbar special area

I like to try to completely take over the area where the NSToolbar resides so I can put my own custom controls, views and background. The advantages of using this area are:
Any sliding panels appear below the toolbar area instead of just the title bar.
In Lion, the toolbar area comes down along with the menu bar when the mouse is at the top of the screen.
I have tried using a borderless window, and implementing my own custom views within it but unfortunately I lose the above advantages as well as having a few other minor problems.
My current method is to use the undocumented method '_toolbarView' with the NSToolbar and add my custom view into its subviews. This works fine as I can turn off toolbar customisation. Unfortunately, the size of the toolbar is initialised with the items within that toolbar. Does anyone know if I can change the size of toolbar without adding a fake ToolbarItem?
Maybe there's also a better way of doing this that I am currently unaware of.
Thanks for any suggestions and comments.
No need to use any undocumented APIs. Just create a toolbar item with a custom view:
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
…
[item setView:myCustomToolbarView];
…
}
You can control your custom toolbar’s size using the item’s minSize and maxSize properties (e. g. in your NSWindowDelegate’s -windowDidResize:).
Remember to also update the toolbar display mode so it doesn't show item labels:
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];

Resources