how to set height of UIScrollView properly - swift

I have a View. Inside that view I have Scroll view. Inside that Scroll View I have Content View. I set constraint that content view height and width is equal to View's height and width. The problem is that I have Navigation bar above of my superview. So when I look at simulator on running time it gives me extra space of that navigation bar inside UIScrollview. So have can I adjust height of content view.
I want to create constratin like this . Content view height = superview height - 66. 66 is the height of navigation bar...

the better way is inside your main view put Navigation bar on top of the view and put your Scrollview below the Navigation bar and inside that ScrollView Put the Content View
and if you want to put Navigation bar under the Scrollview you should specify the top space constraint of Content View other wise it will give the same output

Related

UIScrollView is unable to scroll

I have a scroll view, then inside the scroll view I have a view, and then lastly I have a text view inside the view. I turned off scrolling on the text view so that the more a user types the bigger the text view gets. I then gave trailing, leading, and top constraints to the scroll view. After that I set the view inside the scroll view to 0,0,0,0 for its frame and then I gave top, bottom, trailing, and leading to the text view. Lastly I set the the views width equal to the superview and the scroll view height equal to the view inside of it. In theory when a user types and it goes to a new line the view should get bigger and since the scroll view and view have equal heights the scroll view should also get bigger. However when I go to a new line the view gets bigger but I am unable to scroll, no scroll bar, nothing. Heres some screenshots of the constraints:
Figured it out...
The scrollview height and the view can't be equal, the view should be larger than the scroll view or else there's no point in having the scroll view.

Storyboard Scrollview with StackView inside not scrolling

I know similar questions were asked before but all of them do it programatically and I'm trying to do this in the storyboard.
I have a scroll view with top, bottom, leading and trailing constraints, with a view inside it that is pinned to all edges of the scroll view and has equal width and height #250. Inside that I have a stack view with 6 other views that will change their height dynamically based on how much text each label gets. And it all works fine except for the fact that it doesn't scroll even when content overflows.
What am I doing wrong?
Here's a look at the storyboard:
You need to remove the inner view height which = 250 , and pin the stackView to all it's edges

Get the needed height to display all content within a View

I have a custom view that gets loaded from nib that has a number of subviews. Some of these subviews contain labels which can be a dynamic height. This view ends up being added as a subview to a view that is in a scroll view.
Think ScrollView > View > SubView > Dynamic Labels
I am using auto layout constraints and therefore need to set the height of SubView that is required to display all the content in itself, so that the ScrollView will scroll the length necessary to display everything in the SubView.
I know with a UILabel, you can do something like val neededLabelHeight = label.sizeThatFits(CGFloat(width: label.frame.height, height: CGFloat.greatestFiniteMagnitude)) to get the needed height to display on a single label, but when I attempt to do this on a whole custom view loaded from nib, it just has the height of the view I have in my nib file.
Is there something similar for doing it on a view with more subviews, other than calculating the needed hight of the view by summing all the needed heights of its subviews and vertical constraints?
Here is a simple example...
The XIB file looks like this:
Note: the Bottom Label has a constraint of >= 8 to the bottom of the view. That way you don't have to worry about IB showing an error during design. Any extra space at the bottom will automatically "disappear" at run-time.
The Storyboard looks like this:
The scroll view has a red background (to make it easy to see). The labels and views containing buttons are laid-out and constraints are set as we normally set them.
The "Blue" view - TheXIB Container View - has a Height constraint of 120, but is set as a Placeholder that will be removed at run-time, allowing the view loaded from the XIB to control its height. This will be the view to which we add our Labels Holder View from the XIB.
Here's how it looks with a small amount of text:
It's not scrollable, because there isn't enough content.
After tapping the "Add" buttons a few times, it looks like this:
and it is scrollable (we can see it's scrolled down in this image).
For folks who prefer using a "content view" as the "root" view of a scroll view, it works the same (orange view is the content view, inset by 8-pts on each side):
Here is a link to this example project: https://github.com/DonMag/LoadXIBIntoScroll
And here's a gif animation of it in action:

My horizontal scrollview is not working

I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.

Gap above ContentView / not the same height as UIScrollview - xib

Issue: gap above where contentView starts. Should be 0.
Despite setting the content view top bottom leading and trailing to 0, the content view has a gap at the top. I am using auto layout only.
Scrollview background is blue &
Content view is grey for easy viewing.
If I set the content view to equal heights as the scroll view, I get an error. And this doesn't seem like the right approach away. According to this setting equal height is optional:
Apple auto layout scrollview page
I do set equal widths.
Entire Screen:
Top of Scroll view:
Bottom of scroll view -> this is right - flush with bottom of scroll view (not sure if that matters)
Constraints:
Subview constraints:
Please help!! Also I am using xib files - not sure if that matters.
Thank you!
You'll notice that the gap is equal in height to the navigation bar.
The gap is there because by default iOS assumes that when using a translucent navigation bar, scroll views (and their subclasses like table views) begin at the top of the screen, behind the translucent navigation bar.
iOS then assumes you do not want your content hidden behind the translucent navigation bar, so it applies a top content inset to any scroll view, of height equal to the navigation bar height.
This behavior can be overridden in two ways:
Unmark Adjust Scroll View Insets on the view controller (see image below)
Make your navigation bar not translucent. If you're using a Storyboard, select the navigation controller that contains the affected view controller, and unmark the Translucent checkbox.