Unity stretch scrollview bottom to the bottom of the screen - unity3d

I would like to stretch the scrollview's bottom to the bottom of the Screen.
I tried with
scrollViewItemRt = (RectTransform)scrollViewItem.transform;
scrollViewItemRt.sizeDelta = new Vector2(0, -Screen.height);
But it doesn't work.

First, make sure your ScrollView is a child of a Canvas which covers the screen, and not a child of any sub-object that only covers part of the screen.
In the ScrollView's RectTransform, pick one of the options that makes it stretch vertically:
such as the one highlighted. The ones in the bottom row here, with the blue ↕ character, will all work.
Then, the PosY and Height options will change to Top and Bottom. Make sure they are both 0, or small numbers if you want a little padding.

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.

Unity scrollview can't scroll to the end

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

Autolayout - Modify center align with multiplier

I want to have to buttons, which are horizontally centered in the container. Because they would overlap I want them to be a little offset from the center. I tried it with the multiplier.
If the button is in the center (as normal) the multiplier is 1. So I thought that for the left button, I specify a multiplier of 1,2 and for the right button 0,8.
And also, why is the constraint of the left button moved an the other one not?
I can't speak to the behavior of the multiplier for the center align constraint. If I were doing this, I would:
Create a view to hold both buttons. Drag both buttons into the new view.
Pin the top, left, and bottom of TOR to the top, left, and bottom of the new view, all with constants of 0.
Pin the top, right, and bottom of STRAFE to the top, right, and bottom of the new view, all with constants of 0.
Pin TOR to STRAFE with a horizontal spacing of 20. Fix any misplaced views by updating their frames.
Make the new view's color clear.
Now you can click between the buttons and they will stay together. Center their containing view horizontally and give it a vertical constraint and you're done.

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.

AutoLayout: Vertically centering two UIViews in a superview that can resize

I have two UIButtons, one on top of the other, in a superview whose height can be resized. The two buttons should have a constant vertical spacing between them, but the top and bottom spacing should resize so that the two buttons stay centered in the superview as it resizes.
I tried creating two less-than-or-equal constraints (with equal priority) on the spacing to the superview for each button, as well as a constant vertical spacing between the buttons, as shown below:
(The reason why it's less-than-or-equal here is because this view is defined at the given height in IB for 4" screens, but can be shrunk for 3.5" screens.) However, this doesn't do the trick, as you can see from the screenshot while the app is running:
It's almost as if you want to be able to tell AutoLayout that the two constraints themselves should have equal values, even if they are both set to "less-than-or-equal". Is there any way to do what I'm trying to do, or perhaps a better way?
This is so trivial to do in IB.
1) ⌃ drag from button1 to top. select "center horizontally in container".
2) ⌃ drag from button1 to left. select "center vertically in container".
3) do the same with button2.
4) now the only thing left to do is size the buttons because this is what it looks like.
This is also very trivial.
5) ⌃ drag from button1 to the left. select "leading space to container margin".
6) ⌃ drag from button1 to the right. select "trailing space to container margin".
7) do the exact same thing with button2.
The finished product, looks like this (NB I didn't quite center them, but I could have easily enough):
The simplest way to vertically center is to add a NSLayoutAttributeCenterY constraint - preferably to the element that is near the center of the view. And if all views have a vertical spacing constraints, then they will all be centred. No need to muck with the view hierarchy or add spacer views.
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]];
If you need to adjust the positioning, set the constant. For example: constant:-30 will move it up 30 points.
You can also anchor elements based on different logical areas of the view. For example, if you wanted to anchor your first button at 25% of the view height, you can set the multiplier to 0.5.
Agreed with rdelmar. Here's another option if you want to preserve view hierarchy.
You are currently spacing the buttons at the top and bottom using constraints. Instead, create two empty UIViews, they will be used as spacers. They should be positioned one at the top and one at the bottom of your buttons. Using autolayout constraints, make sure that the height of these two spacer views is always equal. Make sure they are pinned to the top and bottom of the buttons and the top and bottom of the superview, respectively.
In VFL: V:|-[spacer1(==spacer2)]-[button1]-(20)-[button2]-[spacer2(==spacer1)]-|.
You may have to do this in code, I'm not sure if IB can do this.
One way to accomplish this is to enclose the two buttons in another UIView, and center that view in the controller's view. Give the buttons a fixed distance to the top and bottom of this view, and either a fixed distance between them, or a fixed height for the view.