Xcode ScrollView doesn't scroll - ios

I'm trying to implement scrollview using latest xcode.
I have content that reaches 'out of bounds' and when I run the simulator, I'm unable to scroll down to see the rest of my content.
My hierarchy / constraints are as follows..
Are there constraints you need to have on the subviews within the 'contentViiew' ? I don't have additional code in my view controller that manipulates the scrollview.
using Xcode -v 11 & Swift -v 5 <- if this makes a difference

Please clear all constraints on contentView and proceed as follows. You need the following constraints for a vertically scrolling scrollView:
1) Constraint your ContentView to have zero distance from all over sides of scrollView.
2) Equal height and width constraint to scrollView. The height constraint to have lower priority (say 400).
The only constraint for subviews is that they all have constraints set relative to view just above them. Also, the bottom subview should have a bottom constraint for scrollView to scroll.
What I mean by this is that all UIViews should have relative vertical constraints. for example, if going from top to bottom, you have three views - A->B->C, then following constraints are required -
1) top to A
2) A to B
3) B to C
and most importantly
4) C to bottom.
If you get an error in interface builder, you can use ">=" instead of "=" constraint for the #4. I hope that helps.

Add a dummy view(UIView) inside a scroll view with zero width. Set constraint of this view like this.
My dummyView Top constraint = scroll view top constraint
My dummyView bottom constraint = scrollView Bottom Constraint
My dummyView leading Constraint = scrollView Leading Constraint
My dummyView Height constraint = give height according to your need (eg 1500)
My dummyView width constraint = 0
Now whenever you want to change height of scroll view just change height of dummy view.

Related

why scrollview with views is not able to scroll to till last?

I am adding UIScrollView in storyboard with all sides(trailing,leading,top,bottom) constraints. I have added a contentView inside a UIScrollView added constraints all sides(trailing,leading,top,bottom) and Equal width and Heights to SuperView(not scrollView) and changed height constraint priority to 250.
I have kept 3 views insdie content view for all views I have mentioned height and 3 sides(except bottom) but for last view I have kept all four sides and height.
But why i am not able to scroll till last view bottom.
]2
You give the contentView equal height constraint to main view ( Remove it )
While you should hook subviews of it so the scrollView will scroll according to the current content
Hey #santhosh I think you set a Equal height with SuperView is wrong
1. Remove ComentView Height constrains
2. set all(Three view) height and all constrains trailing,leading,top,bottom
Automatically remove(scrollview) constrains error.

UIScrollView height while adding content programmatically in UIStackView

I am trying to make a view containing an UIScrollView. This UIScrollView contains 3 UIViews. The last one contains another UIStackView I wanted to fill at runtime.
Here an image of the storyboard :
But when I add content at runtime in the second UIStackView, the ScrollView's height remains the same.
The second UIStackView is defined as the first one with:
Axis : vertical
Alignment : Fill
Distribution : Equal Spacing
Then I use :
mStackView.addArrangedSubview(matProgress)
The result below :
What's the way to have the bottom view and the ScrollView stretch to fit the content.
The layout can be done with auto-layout and constraints.
The key points are:
do not set height constraints on either StackView
set the "Main" StackView Distribution to Equal Spacing
set the "Main" StackView Leading, Trailing, Top and Bottom constraints to its Superview (which is the ScrollView)
you do need to also set a width constraint on the "Main" StackView to control the horizontal contentSize
The only quirk will be on startup. If you have NO content in the bottom / inner StackView, it will still "exist" in the Main StackView and take up space. There is a trick to get around that, but it must be done in code.
You can see a working example here: https://github.com/DonMag/StackyScrolly
If you have UIScrollView and the child views with auto-layout. Setting programmatically the height of the scrollview will stretch the inside views to comply with the auto-layout.
So, you get by code the height of the third view and set the UIScrollView height to view1.height + view2.height + expectedThirdViewHeight.

How do you set constraints for scrollview that doesn't occupy the whole parent view

I'm trying to build UI similar to that of ios photo gallery : Navigation view controller with a scrollview occupying 80% height and 100% width of the parent's view and the collection view controller occupying the rest of the height and 100% width. So here's how it looks like :
The blue area is scrollview and its content view. The bottom part is a collection view which suppose to behave like a carousel. You can see the constraints that I have set in the following screenshot :
:
I want to set the scrollview height so that it only occupies 80% of the parent view estate and the collection view occupies the rest. However, I can't seem to resolve scrollview constraint issues such as autolayout not able to resolve height/y position of scrollview. As you can see in the above pic, I tried setting the height of the scrollview to 50% of the parent view but the autolayout still complains about not being able to resolve height. If I let interface builder resolve the issue, it just adds spacing to the content view inside scrollview and pushes it down as a result. You can see that in the following screenshots.
Your view heirarchy is correctly setup so thats quite nice and you are on the right track of what constraints to add. I'm going to write all the constraints starting from step 1.
To your UIScrollView add a top, leading and trailing constraint to the superView. Also add a equal height constraint between your UIScrollView and the superView and set the multiplier to 0.8.
Now add your UICollectionView below the UIScrollView and give it a leading, trailing and bottom to the superView. Also add a vertical spacing between the UICollectionView and UIScrollView.
Now add for the contentView inside the UIScrollView. Add a leading, top, bottom and trailing for the contentView to UIScrollView. As soon as you do this, the constraints will break and Xcode will complain. Now what you need to do more is add a equal height and width constraint between the UIScrollView and contentView. Set the priority of this equal height constraint (assuming you want vertical scroll) to something like 250, so that it breaks when the content inside the UIScrollView becomes too large to be displayed completely.
Now as far as that extra spacing issue is concerned. What you need to do is, select the UIViewController that has your UIScrollVIew and then select the attributes inspector for this UIViewController and uncheck the adjust scroll view insets option. For a screenshot, check this.
As i see from above do the following.
Add leading, trailing and top constraint to scrollview.
Add height constraint i.e drag from scrollview to superview and add equal width, in equal width constraint change the multiple factor to 0.8.
Add leading trailing, bottom constraint to collection view with respect to superview and vertical space constraint with respect to scrollview.

Autolayout Parent view height change

I have parent view at top of screen let say TOPVIEW in storyboard which contains some fixed height subview
and scrollview following TOPVIEW
Looks Like
UIVIEW
- TOPVIEW (128) FIXED
- SCROLLVIEW
When keyboard appears i want to set top view to 0 that's why i have taken IBOUTLET for same and set to 0 and top view clips bounds to YES
But constraint breaks of subviews of topview (Vertical space between views etc)
My question is how to solve constraints break due to parent view height in subview ,
i know one that we can set priority low for breaking constraint but is there any other way ?
Thanks in advance
Put the top view and the scroll view in a vertical UIStackView. When you want to hide the top view, set topView.hidden = true. The stack view will automatically move the top of the scroll view up so it fills the space where the top view was.
That is happening because you have set fixed height constraint to the TOPVIEW and you are changing that constraint constant value dynamically, so auto layout would have to break the constraint.
If you have set ideal constraints to the TOPView and ScrollView then you would just need to make two or three changes in your constraints as below,
Step 1:
Change the height constraint (128) relation from Equal to Less than or Equal as show in the image below,
Step 2:
If you have set the scrollView constraint correctly as explained in the link here, then you just have to make one change that is change the BottomSpace Constraint Relation from Equal to Greater than or Equal of the component which is at the bottom of scrollView (inside scrollView).
Step 3:
Create an outlet of Height constraint of TopView and toggle its constant value from 128 to 0 and vice versa.
That's it, it should work now as expected.
Hope it helps you.
Update
Alternatively you can also go for other way as explained below,
Step 1:
Change the bottomSpace constraint priority of component inside TOPView at the bottom of TOPView to 999.
Step 2:
Select the Clips Subviews property from attribute inspector as show in the image below,
To prove this, i have done small demonstration app and you can see the result below,
Orange View has TopSpace to SuperView, Leading and TrailingSpace to SuperView and Height constraint.
Blue View has TopSpace, Leading, TrailingSpace and BottomSpace to SuperView i.e OrangeView, BottomSpace constraint has priority of 999. And it has Height constraint.
This will do the trick.

Using UIScrollView with Autolayout

I am trying to get a simple scroll view to work with autolayout. My SuperView has a Scroll View. Inside the Scroll View is a ContentView and then a ButtonsView.
View (320x480)
|
|__Scroll View(320x480)
|
|__ContentView(320x556)
|
|__ButtonsView(320x175)
The ScrollView constraints to the SuperView are 0,0,0,0 for Leading, Trailing, Bottom and Top. The ContentView constraints to the Scroll View are 0,0,-96,0 for Leading, Trailing, Bottom and Top. The -96 is because my content view height is 556. I have a ButtonsView inside the ContentView. The ButtonView constraints to the ContentView are 0,0,238,64.35 for Leading, Trailing, Top and Aspect Ratio.
I am getting Missing Constraints and Scrollable Content Size Ambiguity errors on my xib.
I do not want to to hardcode the width and height for either my ContentView or ButtonsView as I want the xib to work for iPad as well.
Can someone please tell me what I am doing wrong and how I can get this fixed?
Thanks
This is not a duplicate question. The question above has a scrollview which doesnt work as intended. My question is more about how autolayout rules can be applied to the scrollviews.
You should let the inner contents of your ScrollView to determine its content size. Whether by indicating fixed height and width (which you don't prefer). Or by putting constraints to determine the sizes of inner contents related to the View. So in your specific case, you can add an Equal Widths Constraint for ContentView and View. To adjust height, you can add an Equal Heights Constraint for ButtonsView and ContentView with multiplier of 175/556
You just need to add a Bottom constraint in your ButtonsView to the ContentView. With those constraints, now the ContentView can guess its height, and so the ScrollView, too.

Resources