Unity scrollview can't scroll to the end - unity3d

I have a scrollview.Horizontal and vertical scrollbars are removed because it is ugly and I have space constraint.
When I populate items into content of the scrollview as shown in the image
I can't scroll. The view is moving but doesn't scroll.
When I scroll, just scroll to some extent and scroll back to the original position.
What could be wrong?

Check if you have Vertical checkbox enabled on Scroll Rect component.
On your content object after adding ContentSizeFitter, you need to select either Min Size or Preferred Size to make the scroll view content automatically resize. The unconstrained option doesn't drive the height so your scroll view won't scroll.

Make sure that the Height parameter in the Content object is not equal to zero:
What happens when you press play:
Now let's set Height to something much bigger than zero:
Here is what we get when we press play this time:
(The scroll bar appeared!)
Credit: Unity scrollview can't scroll to the end

Related

Can't scroll using Scroll View

Whenever I press play, I can't move the scrollbar or the scroll view. The scrollbar also resets for some reason...
I was trying to make a questionnaire but it just doesn't work. And yes, I have set the viewport and the scrollbar, and the react transform.
you have to add vetrical or horizontal layout group to content gameobject under scrollview>viewport>content. also you have to add content size fitter and set it to preferred size for vertical or horizontal. then your scrollview will work.

What am I doing wrong with this Scrollview?

I am attempting to set up a scroll view and every video I watch they say the same things:
•Make scrollview in VC
•Make it bound to all margins (0,0,0,0)
•Embed a content view inside of Scrollview
•Content view margins are (0,0,0,0)
•Set Equal heights & width from content view to superview
•set priority of height to 250
I have done this time and time again and cannot figure out why it still isn't working. Can someone please double check these screenshots?
Try adding some subviews into your content view and make sure to have a complete set of vertical constraints from top to bottom.
If you go in the Size inspector under the "Scroll View" Section you can find the "Content Layout Guides" tick, disable and the errors will disappear
If you Set All the constraints to ScrollView and want to set its contents in code .. it remains complaining about its constraints because they remain ambiguous .. so to remove error from story board
what you do is set Ambiguity to never verify and error will go away .... hope it will help
Looks like you do not have anything bound to your Content Layout Guide.
So basically you have two guides in Scroll View: Frame and Content.
Frame Layout Guide provides you the ability to bind to your UIScrollView's frame. E.g. equal width between your Content view and Scroll View itself.
The most useful here is Content Layout Guide. Basically it helps to determine the scrollable content size of your UIScrollView's contents.
So in your case you have to have a Content view inside your Scroll View that has equal width with Frame Layout Guide and have its edges pinned to Content Layout Guide. Like so:
Oh, and do not forget to have your Content view have a determined height so your scroll view will calculate the size of its scrollable contents.

Vertically centering UILabel programatically in scrollview that doesn't match the view swift

I have a horizontal scrollview that fills 1/3rd of the view.
I want to put a UILabel in the centre of the scroll view, which I have successfully done with:
let reagentsLocationx = mainscrollview.frame.width/2-100
However, I am struggling to centre the label vertically within the scrollview. When I use similar code for reagentsLocationy it centres the label in the view and not within the scroll view.
How do I centre the label, in Swift, just within the scrollview?
Many thanks.
I think this is because you are using frame and not bounds. I don't believe frame will ever be larger than your screen space, but bounds it equal to the view's actual size. Try basing it on the bounds of the scroll views height.
UIScrollView().contentSize.height might be what you are looking for.
No need of setting sizes in code.
Everything done in the storyboard.
Drag your scrollview into your view controller.
Set top, bottom, leading and trailing constraints from the scrollview to the parent view or safe area.
Drag your text label into the scrollview.
Set your top, bottom, leading and trailing constraints to the scrollview.
IMPORTANT set a second leading and trailing constraint, now from the text label to the parent view (UIView - not scrollview) or safe area.
This makes sure that the text label knows his width and so the scrollview knows it too.

UIScrollView Causing "Misplaced Views" AutoLayout issues

I'm running into a strange AutoLayout related issue when I use a UIScrollView (the issue does not occur without it).
I have a UIScrollView that is constrained to the boundaries of a UIView (contained within a UIViewController), and within that, I am attempting to place a UILabel and UITextField side by side. I have constrained the UILabel to the left and upper boundaries, with it's width and height constrained (see screenshot below):
Right next to this UILabel is a UITextField, which is constrained to the left, top, and right, as well as having the height constrained. However, this results in a "Misplaced Views" warning, that states "Expected width = 163, Actual width = 413", shown in the screenshot below:
When I choose to "Reset to Suggested Constraints", the "Misplaced Views" issue disappears, but in it's place I am left with a width constraint of 413 points, which is something I'm hoping to avoid, as I would not like this UIViewController to be horizontally scrollable on smaller devices.
A scroll view has a size (the size it takes up on the screen) and a content size (the size of the entire scrollable area). In Auto Layout, the content size is automatically computed from the constraints of the items in the scroll view. This is a problem, because you are trying to make the scroll view have the same width as your screen, and then have the items constrained to that. When you do that, Auto Layout insists that you give your text field an explicit width so that it can calculate the width of your scrollable area.
To do what you want, do the following:
Add a "content view" to your scroll view. This view will be the only top level item in your scroll view. It will hold all of your content as subviews of it. Drag out a UIView and add it to your scroll view. Constrain its top, leading, bottom, and trailing edges to the scroll view. Constrain its width to the width of the scroll view. Give it a height constraint and set it however big you want your content area to be.
Add all of your labels and textfields to this content view. Now you can constrain them centered in your content view or constrained to the edges, and it will work as you want.

How do I set the right margin of a UITextView?

I have a vertically-scrolling UITextView that fills the width of the screen. I need the text view to have margins (contentInset) of 20 pixels on the left and the right. But for some reason, I can't get the right-hand margin to work, either in Interface Builder or in XCode.
The reason I can't just make the text view narrower is because I am adding a subview which needs to run the full width of the screen. Also, I can't turn subview clipping off, because it plays havoc with a load of other elements on the screen.
Anyone know why the contentInset property is not affecting the right margin?
Thanks!
You could add an intermediate view that can be the superview to your text view and what was previously it's subview.
New View with Frame(0,0,width,height)
->TextView with Frame(20,0,width-20,height)
->Subview with Frame(0,0,width,height)