How to implement browser toolbar in Cocoa? - cocoa

It seems like the best approach is to use NSToolbar. However, it is not obvious to me how to make an NSTextField within an NSToolbar flexible in terms of width. Is this possible? The default behavior of the textfield is to remain a constant width, even if the toolbar is resized.

Make sure you set maxSize and minSize correctly on the toolbar item, and then set the autoresize mask on the text field itself. You control the sizing of a view in a toolbar just as if it was in a window, either make sure its width is flexible in IB if you're creating it there, or specify NSViewWidthSizable if you're creating it in code.

Related

How to make NSSearchField take up whole width of toolbar

I am trying to make a NSSearchField taking up the whole width of a toolbar in a NSWindowController.
I created a new NSWindowController using Xcode storyboard, add a toolbar to it, when add search field to the toolbar.
I drag the search field from allowed toolbar items to default toolbar items
Set toolbar item's max width to a large number say 1000
I build the app and run. The search field shrinks when window width shrinks, but does not expand beyond a certain width when window width expands.
The question is how to make search field expand and take up all remaining space of the toolbar?
It doesn't sound to me like the toolbar is really what you want to use here. The purpose of the toolbar is to allow multiple UI elements to be included in a user-configurable way. If you want to force one element that takes up the entire width, I suggest just putting it in the window's content view and setting up the layout constraints to pin to both the left and right sides. If you set the "Textured" check box in Interface Builder, it should look roughly the same as it would have looked using the toolbar.

How do I make a WrappingTextField fill the entire Window?

I have a Window and a WrappingTextField.
I want the WrappingTextField to always fill the entire Window. Even when the user is resizing the Window, and when it is in full screen.
No matter which options or constraints I choose in Auto Layout, it only resizes with the Window horizontally, but its height remains the same, even if the height of the Window grows.
How do I make it fill the entire Window, and stay that way?
By WrappingTextField I'm assuming you mean an a standard NSTextField object with it's Layout set to Wraps within Interface Builder.
The resizing behaviour of an NSTextField is influenced by its Content Hugging Priority. By default Interface Builder sets this at 750 for the control's Vertical dimension, and this is probably what's preventing your text field from resizing the way you want it to. Give it a smaller value - like say 100 - and the problem should resolve.

Can't align label on a Segmented Control in an NSToolbar

As you can see from the screenshot, the View button text is aligned properly under Allowed Toolbar Items, but once I add it to the toolbar, it's aligned to the right. Why?
You have to set sane minimum and maximum sizes for the toolbar item. I think you also have to make sure the autoresize masks (or constraints if you're using automatic layout) are set properly in the toolbar item's view (the NSSegmentedControl in this case). I don't have the "proper" settings handy but the min/max toolbar item size + correct autosizing behavior are the key here.

Can't Edit NSWindow's Toolbar - Cocoa

So I'm using the method:
[someWindow setContentBorderThickness:24.0 forEdge:NSMaxYEdge];
But I can't seem to get the toolbar to increase in height. It simply stays the same as in default. Can anyone shed some light here?
An NSToolbar is automatically resized to accommodate the height of the tallest NSToolbarItem. The standard (large) toolbar items are all 32 px tall, so the toolbar has no need to make itself larger. If you do something like add a custom view toolbar item, then it will be resized to accommodate that item, as shown in the image below:
(To accomplish the result shown above, I clicked on the toolbar twice in IB to bring down the Allowed Toolbar Items sheet, then dragged an NSView custom view from the library palette onto that sheet).
P.S. I'd recommend using this capability with discretion.
You cannot specify an arbitrary height for NSToolbar. You can, however, specify a size mode. A toolbar with 24x24-pixel icons has a small size mode:
[toolbar setSizeMode: NSToolbarSizeModeSmall];
which is equivalent to Size: Small in Interface Builder’s Attributes Inspector.

How to change the height of an NSProgressIndicator?

By default, cocoa progress bars are slightly fat and I want something a little slimmer, like the progress bars seen in the Finder copy dialog. However, Interface Builder locks the NSProgressIndicator control height to 20 pixels and my programmatic attempts to slim down aren't working, as calls to
[progressBar setControlSize:NSMiniControlSize];
and
[progressBar setControlSize:NSSmallControlSize];
in awakeFromNib don't do anything, and the suggestive looking NSProgressIndicatorThickness seen in the header file doesn't seem to plug into any methods that I can see.
What's the trick?
Those calls should have worked. In Interface Builder, in the Geometry pane (the one whose icon is a ruler), there is an equivalent control size selector that offers "Regular" and "Small" sizes.
in IB
select your NSProgressIndicator control
in the utilities view select the View Effects inspector
press + in Content Filters
select Lanczos Scale Transform filter
set the appropriate scale value in the Scale row
set the Aspect Ratio too if you need to change the height only

Resources