UIScrollView bounces back when let go - ios

I have a followed this answer Putting a UIScrollview in a viewcontroller (storyboard) to embed a scrollview in a viewcontroller (Mine is vertically instead of horizontally).
All the constrains are working except the fact that the view bounces back when it is let go. I have tried a lot of different solution, but none is working! Please help, I'm using autolayouts my main goal is to be able to show all the buttons on the screen through scrolling.
EDIT:
I have been trying all types of ways to increase the contentSize, but nothing happens - the scrollview won't scroll properly. I tried different codes from people with the same problem, but the bouncing won't stop. Am I doing anything wrong on my constraints? This is extremely frustrating, please help. My constraints (using autolayout) (as I said, I followed this answer Putting a UIScrollview in a viewcontroller (storyboard))

You have to increase your scrollView's contentSize to fit all of it's sub views.

You should add subview to scrollview, then add all other views to that subview.
scrollView
contentView
subview_1
subview_2
Then add constraints between scrollView and contentView (leading, trailing, top, bottom) and contentView should have width and height constraints set properly (set width equal to scrollView width)... the content size will be calculated automatically.... just set height

Related

Can't get UIScrollView to scroll on Swift Storyboards?

Hey guys,
I'm trying to get a UIScrollView to work here but it doesn't really work? I can't scroll it.
Here's what I've had:
View
- Scroll View (set constraints to be 0,0,0,0 to View)
-- contentView (set contraints to be 0,0,0,0 to scroll view, equal widths and heights as contentView, priority 250)
but I still haven't been able to scroll it? I've seen all tutorials and followed them and there is nothing that can fix it. Is there a setting that I have been missing? Is there something I might have accidentally toggled? Thank you in advance.
There are several things that you can tweak:
Go to Size Inspector of the View Controller (in your case - Edit Programmes View Controller), change the Simulated Size to Freeform.
Turn off the Content Layout Guides in the Size Inspector of the ScrollView
Add 0 Constraint to Top, Bottom, Leading, and Trailing
The contentView that is inside your ScrollView must have an equal Width with the ScrollView.
Then add Height Constraint according to the number that is already inside it.
I hope it can help.

UITextView not visible on UIScrollView

Ok, so what I have is a UIScrollView that is constrained to all four sides of the main view, centered both vertically and horizontally, and set to have equal width and height to the view. All of the subviews that I put on top of the UIScrollView are showing up when I run the app, exactly where I want them to be, but only the UITextView at the bottom is not. It seems like I've tried every combination of constraints but it never appears when I run the app regardless of what I do. Here is a screenshot of the constraints in the interface builder:
And even when I preview the file Main.storyboard before running it looks like this:
But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:
Any help with this problem will be greatly appreciated! Thanks in advance.
Avoid putting all your subviews directly inside the scrollView. The autolayout will break apart.
You need to :
Add a UIView inside the UIScrollView, with constraints 0-0-0-0 to leading-top-bottom-trailing to the UIScrollView, and put all your subViews inside that UIView.
After that, you need to set the contentSize of your UIScrollView by code.
Also, you can:
add missing constraints to see what is missing.
The left panel in UIStoryBoard (Document Outline), you can press the red arrow showing up to see what is missing or conflicting.
Check out Apple documentation for more details.
If you want your scrollView only scrolls vertically you shouldn't set its height equal to its superView so remove it and just set the width to its superView and then it should calculate the height based on the subViews inside it
I offer you to drag a UIView in your scrollView and set the constraints to its four sides, and name it containerView , then set its width equal to background view and start laying out your views inside it not inside the scrollview :)
If Height of all views in the scrollView is clear, it can infer the scrollView's height
in this case you can set a fixed height to your containerView like 800 to get rid of the red lines and check how it works :)
I have noticed some clues you might have to solve which could probably solve the issue. Since the scrollView is extend by its content's elements, you have to explicitly deal with each element in the scrollView:
For imageView on the top is not well constrained, you need to give it a width or aspect ratio. Fix the imageView issue might solve textview height problem, even it's not still is a good start.
TextView bottom anchor is equal to scrollView bottom anchor, but you have to know textView is also kind of scrollView. So it's not reasonable to constraint each other at same time. Because both of them don't have explicitly height. You can try to type some words in the textView which will at least give it height by its content, then the scrollView can detect the textView bottomAnchor. You might see something then.
Your scrollView's height is equal to view's height is also weird, scrollView shouldn't constraint its height at first. Because it can't be "Scroll"View anymore because it's height is constant. you should let its width equal to view, and let the height be decided by its element's height. Then it will be literally a scrollview.
Hope it helps

Not able to set ContainSize of my ScrollView through Storyboard

I am adding more than 8-9 controls in my ScrollView but I am not able to set ContaintSize of that ScrollView so that I can able to scroll and view all controlls.
I HAVE SET below constrains to my scrollview and add some controls into my scroll view with the help of contain view.
Scrollview.top respect to his superview
Scrollview.bottom respect to his superview
Scrollview.leading respect to his superview
Scrollview.trailling reapect to his superview
Than I have added UIView as contain view and set all constrain respect to scrollview.
Than added all controls into view but still its not scrolling as expected.
By below code I am able to set containview but its ststic one and cant assume that 1000 is my contanent height.
Scrollview.contentsize = CGSizeMake (Scrollview.frame.size.width,1000);
Above part is static as I have many lable with multiple line in it.
All data is dynamic comes from server si cant set it static.
Can any one help me out how to set autolayout of scrollview so I can get dynamic content size?
Edit: some time I am getting autolayout error into storyboard like below.
Scrollview has ambiguous scrollable content height
An while i reslove conflicts its not showing proper scrollview in my screen.
Faced the same issue recently. I'm sure that you did your research but here is the guide I used, nonetheless.
https://www.natashatherobot.com/ios-autolayout-scrollview/
Also if you have many labels it would be good consider using a 'UITableView' or a 'UIStackView'.
Edit
Make sure you center the scroll view vertically and horizontally in respect to its container.
You have to define constraints for the scroll view like this..
ScrollView :
top ,left , bottom, right pinned to superview.
ContainerView:
top, left , bottom , right pinned to scroll view
width pinned to main view
And make sure that every subview inside the container view should have constraints in such a way that it should define the height of container view.
As i understand your issue you forgot to set the height constraint of your contentView, just try by setting it.
No need to set the fix height of scrollView's contentSize, please see the below code.
self.scrollview.contentSize = CGSizeMake(self.contentView.frame.size.width, contentView.frame.origin.y + contentView.frame.size.height + 10);
Note: you need to add height and width constraint of contentView if you need scroll according to it.

Objective-C UIScrollView not scrolling

I have never used UIScrollView before, so I apologize in advanced if my question seems dumb.
I add a UIScrollView to my storyboard scene and then I added labels inside my the scrollview. But when I run my app nothing scrolls. and my text runs off the app screen. Here is a screenshot of the list of items inside my storyboard scene
My question is, is there any steps I may have missed to get this working?
Set this in code:
// the size dimensions should be larger than the scroll view's frame size.
scrollView.contentSize = CGSizeMake(xSize, ySize);
Your problem is because scrollView has contentSize property which is not set yet. So it's not scrolling. To do it in code you should try out David's answer to try it in storyboard, you should add these constraints
These two constraints will make height-
scrollview vertical space to toolbar(bottom constarint)
scollView vertical space to launchImage(top constraint)
These two will make width
scrollView leading space to container/superView
scrollView trailing space to container/superView

UIScrollViews subviews not resizing themselves on Portrait to Landscape Orientation Change

In my app, I have a hierarchy like Main view -> UIScrollView - > UISearchBar. The search bar covers the whole width of screen. But when it switches to landscape the scrollview adjust itself according to landscape and cover whole screen but its subviews like UISearchBar width remain same as in portrait. Plus I'm using Autolayout and I've correctly set their constraints (Leading and Trailing, width (greater than equal to 320) etc) in storyboard.
Also I know there are several answers for this in SO but almost everyone is saying set width again in willAutororate. But I have around 15 subviews in UIScrollview which I think is not possible to set programatically (as increase LOC) plus I think Autolayout must have some solution to it.
Scrollviews don't play very well with constraints. Here's an approach using constraints and Interface Builder that worked for me for having a vertically scrolling scrollview that doesn't require any manual or content size finagling and works with rotation:
Scrollview in a view controller positioned with constraints
A container (UIView) that is a subview of the scrollview positioned with constraints pinning each edge to the superview
Add all your additional subviews to the container view instead. Use constraints. Exceed the height of the scrollview to get a vertically scrolling scrollview.
There is one trick however to get rotation resizing to work automagically:
You have to add an additional constraint to the container view to match its width to the UIScrollview's width. It's strange that you have to do this, since pinning each side to the scrollview should do the trick, but alas it won't work with rotation if you don't add the additional constraint.
Here's what I mean:
try this :
this problem will solve by constraints only. I created a project and by using correct constraints its working fine for me ,
here I am going to show you how to add correct constraint one by one :
add scrollview and add constraints for scrollview only like in the screenshots :
2. now add search bar and add constraints for search bar too,
here below you can find out complete constraints for scrollview and search together:
You could use UIViewAutoresizing mask (autoresizingMask property) of your subviews.

Resources