How to make sure views do not get clipped when window is resized and the size of views become smaller or bigger with the window size? Is there a way to do this other than using an autoresizing mask?
The views in a window i have are getting clipped if the window is made smaller, i want the size of the views to decrease with the decrease in window size. How can this be done using constraints?
You should constrain the views to your window edges (their topmost superview in the hierarchy).
First you click and drag holding Ctrl from your view to the content view of the view controller (or window controller):
Then you make the required constraints so that your views are not clipped (those would be trailing, leading, top and bottom constraints):
Related
I am creating a view with four table views in it. I want each table view to take 50% of the height and width of the containing view and to auto-resize when the window is resized.
I have been unable to find a way to do this thus far.
You should use autolayout in storyboard. Resizing and stuff like that becomes very easy that way. Basically you want to set constraints for each tableview to the edges which they are touching and their neighboring tableviews (distance: 0). Furthermore you have to set constraints for equal width and equal heights.
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'm having resizing issues, and I think it's because I don't really know enough about frames and bounds.
I have a custom view within a scroll view, which fills the window. When I resize the window, I want the custom view to stay where it is, slowly getting covered/uncovered by the window in the place that the mouse is dragging.
What really happens is the custom view stays anchored to the lower left corner of the scroll view, so that if I make the window shorter, the custom view slides up to keep its lower left corner touching the scroll views corner.
How do I resize the window without moving a particular view?
The frame is the area that the view will occupy within its parent. The bounds is the section of the view that will be drawn within its frame. So 99.99% of the time that the two differ at all, they have the same size but the bounds has a zero origin and the frame has a non-zero origin.
That said, it sounds more like you're confused about the coordinate system. OS X follows the graph paper convention of the origin being in the lower left hand edge of the screen. So your scroll view's origin is in the lower left of the window, which results in that point being the anchor when you resize. The size of the scroll view's frame and bounds changes but the origin doesn't.
Assuming you want the top left to be anchored rather than the bottom left (?), possibly the easiest thing to do would be to subclass NSScrollView and override - setFrame: to do appropriate arithmetic — grab the current documentVisibleRect, work out what's in the top left, allow super to set the new frame then call scrollToPoint appropriately.
If you want to pin your document view to the top left, you can override isFlipped in your document view to return YES. In that case the y coordinate will be flipped and you may need to perform some computation adjustments.
- (BOOL)isFlipped
{
return YES;
}