How do I make NSScrollView size its document view for the total size of its contents using constraints? - appkit

Say I have a Mac app with a window containing an NSScrollView. When added to a storyboard or nib file, the scroll view contains a clip view (NSClipView) and an NSView that is the scroll view's document view.
I want to add some content to this document view (say, two labels arranged vertically) and set up constraints such that the scroll view will scroll as it needs to in order to reveal all the content. So:
Pin the top, leading, and trailing of the upper label to those if its superview (the document view)
Pin the top of the lower label to the bottom of the upper label
Pin the leading, trailing, and bottom of the lower label to those of its superview (the document view)
Add a custom view to the document view, pin its bottom, left, and right edges to superview, and constrain its width to a constant (the width of the window) and its height to 0 to define the scroll view's scrollable content width
At this point, if I set the text of the labels to something long enough to overrun the height of the window, I'd expect the scroll view to allow the user to scroll to reveal the rest of the content. But it doesn't - it just elastic-scrolls vertically, and the text is cut off. Even if I set the content compression resistance to 1000 on both of the labels.
What am I missing here?
Sample app here: https://github.com/tomhamming/MacScrollTester

Related

Dynamically increasing height of content view of a scroll view in ios9

I am struggling with this stuff for a few days. I am having a view controller in which I have a scroll view and above it a content view. In the content view I am displaying some text from web in a label. I am using a view controller of free form size 600*1000 for my designing purpose.I have given the following constraint to my content view -
1)pinned to the edges of scroll view.
2)content view width equals to superview width.
3)content view height equal to 900.
My scroll view is working perfect.
So now I want that whenever my text increases then my label size would increase thereby increasing the height of my content view automatically. So how to do so. Any answers are most welcome. Thank you.
Set the number of lines for your UILabel to 0.
Add leading, trailing, top and bottom constraints to UILabel.
Set number of lines to 0 does the trick, as the content increases UILabel will automatically adjust the height.

UIScrollView contentInset is set to nonZero when using autolayout

So I have the following view hierarchy :
A full size scrollView in my viewController's view with the following constraints :
These are the constraints on containerView (ignore the second last one, its a hacky semi-fix for my problem):
I have the status bar, the navigation bar and the tab bar visible.
The thing is that when I set a breakpoint to check the scrollView's contentInset, it shows 64 on top and 49 on bottom, left and right are zero.
There is no way to set contentInset in IB, I tried setting it in an IBAction to UIEdgeInsetZeio, but that didn't fix it either. This is screwing up my scrollview by adding space above and below my contentView, how can I fix this?
By default the view controller extends the view under the top navigation bar so your content will blur under a translucent navigation bar. This is controlled by edgesForExtendLayout which is managed in Storyboard via the Extend Edges setting.
By default, the scrollview will automatically adjust its content inset so the content appears below the top layout guide. This is controlled by automaticallyAdjustsScrollViewInsets which is also managed in Storyboard.
What you did was constrain the top of your scroll view to the top layout guide instead of the top of its superview. By doing this, you manually offset it by 64 points. However, the scrollview is still automatically insetting its content by 64 points, which is why you're seeing additional space above and below your scroll view.
Either constrain your scrollview to its superview (so its content scrolls under the top/bottom bars), or disable the view controller from automatically adjusting the scroll view inset.

How to make an item full width in auto layout constraint?

I'm trying to make a button full width in ios 9 storyboards, and am trying to create a width auto constraint, but the width constraint only seems to take an exact static number. How do I make the item go full width.
Additionally if I wanted say an imageview to be full height and adjustable width to maintain it's aspect ratio - or a size to fit fill, how would I do that with the auto layout constraints in the storyboard.
Should I be doing this programmatically in the view controller?
For the button:
In storyboard drag the left and right sides of the button to make it full width. Make sure the button is selected and add a pin constraint to the left and right (if you've dragged it full width the pin constraint should be equal to -20)
For the imageView:
The full height constraint is done almost exactly the same, although this time you need to drag the top and bottom to the top and bottom edges of the view controller and pin the top and bottom to their respective Layout guide.
You can make the image full screen in the view controller (pin top, bottom, left & right) and then edit the aspect ratio of the image in the Attributes inspector when the imageView is selected:
Hope that helps!

iOS fixed width dynamic height scrollview with autolayout in xib

I was creating a scrollview contains an image view and a dynamic height label,
here's my hierarchy in xib file:
-> view (originally existed view when xib file created )
--> scroll view
---> content view
----> image + label (dynamic height depend on its content)
I set the content view's height depend on the label's height
,and then set the scroll view's content size(scrollable area) to the content view's frame size
I expect the content view and the scroll view both has equal width to the top view
(top view's width is flexible when device got rotated, and also are the content view & scroll view's)
and only the vertical direction is scrollable.
yet I totally have no idea dealing with the autolayout,
once I set the content view's constraints to the top view, the scrolling becomes disable,
but after I remove all constraints the content view'd be scrolled like a bouncing block...
(sorry for not providing any picture but only description for my question due to low reputation)
been mad about this for a whole day
any help would be appreciated.

How to ensure NSOutlineView inside a custom view renders full height (without scrolling)

I have a window with two custom views stacked on top of each other. I use auto layout constraints, as follows:
Top view leading, top, and trailing edges are tied to superview.
Bottom view leading, bottom, and trailing edges are tied to superview.
There is a fixed vertical space constraint between two views.
Bottom view has a fixed height constraint.
Top view has a it's compression resistance set to 751.
So far so good. The idea is that the bottom view height is fixed and as the window is resized, the top view height adjusts to compensate.
Both top and bottom views have other views/controllers loaded in, as appropriate. The bottom view ends up containing an outline view with a half-dozen entries. I would like that outline view not to scroll. In other words, I would like to automatically adjust my bottom view fixed height constraint to match the height of the outline view required for it not to scroll.
Ideally, I'd prefer to solve this with auto layout without having to write code. But, if that's stretching beyond what auto layout can do, code help would be appreciated as well.

Resources