I've got a pretty dynamic UI that uses a NSSegmentedControl in the toolbar of its document window.
The NSSegmentedControl dynamically gets its elements assigned.
The number of elements can vary widely between 1 and maybe 15.
At the moment the toolbar will not adjust for the number of elements in the NSSegmentedControl, i.e. after setting the number of segments via setSegmentCount: all segments that exceed the original width of the NSSegmentedControl (at the time the window was created) are clipped:
What would be the best way to tell the NSToolbar to recalculate the width of all its elements, i.e. the width of the NSSegmentedControl and the placements of all elements right to it?
The clipping is caused by NSToolbarItem's current value for -maxSize. Try adjusting its width with - setMaxSize: after you add segments to the NSSegmentedControl or directly set the maximal width in Interface Builder.
There is more info on this topic at https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Toolbars/Tasks/SettingTBItemSize.html
Related
I have a simple custom view and within a Wrapping Label (Multi Line Label) as a subview. I used autolayout to set the constraints.
Now I want that the superview change its size to show all content of the wrapping label. How can I achieve that? The wrapping laben shows the content of the extra text field (Outlet: "textfeld").
You need to set the content hugging & compression resistance constraints on your label (among other constraints).
With the label selected, select the size inspector. At the bottom, you'll see the Constraints section:
These have default values of around 250 & 750. We need to boost these up to 1000. These constraints control the way that the label grows & shrinks relative to its content, but with such low priority, every other autolayout constraint you add will take priority. So we need to increase the priority of these constraints up to 1000 which marks them as required (or at a minimum, you need to increase the content compression resistance priorities).
Now you just implement other autolayout constraints around that which make sense. For example, if you always want it to have at least a 20 pixel border from each side, be 20 pixels from the top, and centered in your superview, you might set up constraints like this:
Of course, with these constraints, if the parent view isn't constrained to a max width, you won't have implicitly forced any word wrapping, so it might expand the parent view's width. We can add a max width constraint to our label too.
So I just returned to GUI programming after a long time on Mac OS X. I heard about this great feature called Auto Layout. For my project I want a very simple layout: a textfield dominating the window, with a couple buttons at the bottom. When I resize the window, I want the textfield to resize with it.
I thought that was simple task: constraints on the 4 edges and Height and Width >= what I have in Interface Builder:
If I set it like this, I can't resize the window vertically, only horizontally. If I drop either the Height or Width constraint, it will shrink to a tiny size in a corner.
How should I set my constraints so that the textfield resize with the window?
To resize text field with the window size you need to add constraints to all the four edges of the text field! As per your screen shot you haven't added any constraints to the right and bottom edge, instead you have added dimensions (or say height and width)!
And to avoid making text field terribly small you can add min-width and height to it.
I have an NSSplitView with content in both NSViews. The left NSView has 2 constraints – Equal Widths and Equal Heights. The right NSView has something simple, say an NSTextField, which is centered via constraints Center X Alignment and Center Y Alighment. This is what I hoped it would look like as I resize the window and/or the NSSplitView divider:
This is what's happening:
I've tried a great deal of configuration changes, I've tried using an NSSplitViewController vs just dropping an NSSplitView into an NSViewController to adjust more parameters programmatically, but I'm not having any luck. Whenever resizing the window, the left view always takes over the excess space. The same happens with the divider (it can be resized, but letting go of the mouse button causes it to snap right back). It seems there's something fundamental that I'm missing here.
The text field's content hugging priority is probably higher than the split view item's holding priority. Fix that and the view should probably work the way you expect.
Also, if, when you resize the view, the left view is resizing with the window while the right view stays the same size, then that suggests that the left view's holding priority may be higher than the right's. You should make the side that you want to stay the same size have the higher holding priority.
That said, I'm not sure what you mean about the constraints you've set on the subviews. "The left NSView has 2 constraints – Equal Widths and Equal Heights." What do you mean here? Its width is equal to what? Its height is equal to what? Do you mean it has an aspect ratio constraint? Frankly, I can't think of what constraints of those kinds would make sense for a view within a split view.
I have an NSView inside my main view that is preventing my window/main view from resizing correctly. Even when I go fullscreen, My main view can't fill the screen (there's some black space at the right). I know that the cause is that particular view (or something inside) as when I delete it my app behaves normally. What would cause an NSView to "control" growing of its window?
Here are the constraints on that view:
UPDATE: I've found an NSTextField inside which had hugging proiorty set to 750. I've taken it down to 250, now it DOES grow, but I can't shrink it down from "some" size. I'm calling it "some" size because it has nothing special: it's a bit less than my native fullscreen width, and it's different than my IB width.
In general, constraints with priorities higher than NSLayoutPriorityWindowSizeStayPut (500) can force the size of a window. That includes the implicit constraints generated by intrinsic content size, if a view has such, which have the priorities set for content hugging and compression resistance.
So, if you have a text field whose horizontal content hugging priority is, say, 750 and there's a chain of constraints that connect its leading and trailing edges to the window's content view's edges (or, similarly, relate the text field's width to the content view's width), then the window won't be able to grow large enough to "stretch" that text field.
Likewise, if the text field's horizontal compression resistance is high, the window won't be able to shrink to the point where the text field would have to be compressed.
I am using Xcode's auto layout feature for the first time in a project where I have several NSPopUpButtons.
Now what I want to achieve is to have two popUpButtons in a row together with their labels and when the window is resized I want both popUpButtons to adjust their width while keeping the horizontal spacing between each other.
However no matter how I apply the constraints I just don't get the popUpButtons to change their size with the window. They will always break their horizontal spacing constraints and just increase/decrease the spacing to the labels. I hope it gets a bit clearer what I have done from this screenshot:
I have set the spacings between the labels and the popUpButtons to fixed values with 1000 priority and have set the width constraints fo the popUpButtons to be greater or equal to the initial size.
How must I set my constraints to have the popUpButtons resize?
While writing this question I realized what the trick is:
In the size inspector of the NSPopUpButton I had to reduce the Content Hugging Priority.
Obviously this controls how closely the view wants to 'hug' its content. So when the hugging priority is higher than the resize priority the view will not want to increase its size because that would mean to have more empty space between its bounds and its content.
Then in my special case, I could also pin both NSPopUpButtons to have the same width and voilà: the popUpButtons will perfectly resize while keeping the spacing constant.