I have this scroll view with many elements inside for which the height is variable.
I want to know if there is a way to know the maxY of the last element in the scrollview, without adding up the height of all the elements in the scrollview.
I tried lastViewInScrollView.frame.maxY and it was not right.
It is totally mathematics, as you said you dont want to add all the element before the last element, thats OK, now work with the end of the scrollview.
As per the apple documentation (https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html), to properly deal with the scrollView you have to add a UIView (let says myView)and then your components.
Assuming you have scrollview which contains 10 UIView (view1, view2, ... view10 ) of different height and your viewController is like below:
Dealing with Constraints of UIScrollView:
scollview constraints :
a) leading to superview : 0 //superview(VC mainView)
b) trailing to superview : 0
c) top to superview : 0
d) bottom to superview : 0
keep a uiview(says myView) inside the scrollview and give consraints:
a) leading to scollView : 0
b) trailing to scollView : 0
c) top to scollView : 0
d) bottom to scollView : 0
e) equal width to the mainview // assuming as you dont wanted horizontal scrolling otherwise, dont give
Now add view1 in myView, add contraints
a) leading to myView : 0
b) trailing to myView : 0
c) top to myView : 0
d) constant height = height1
now, view1.frame = (0,0,scW,height1) // scW is scrollViewWidth, and you dont need to set this frame. I just write here to describe you the frame of view1.
Now add view2 in myView, add contraints
a) leading to myView : 0
b) trailing to myView : 0
c) top to view1 : 0
d) constant height = height2
:
:
:
:
:
Repeat same to upto view9,
Now add view10 in myView, add contraints
a) leading to myView : 0
b) trailing to myView : 0
c) top to view9 : 0
d) constant height = height2
e) bottom to myView : 0
If you facing problem how to set frames, specially when they are out of controller view, then select you VC goto its property and set size freedom and set any height of VC so that you can set all the frames. DONT'T FORGET TO SET THIS VC SIZE TO INFERED.
NOTE: Never give the static height to the scrollview, scrollview will itself calculate its height by calculating the height of its components, but keep in mind you have to give top most component top constaraint to its superview and lower most component bottom component and height of all the components (here, UIView).
Try this above procedure it is good practice for new i scrollview.
HOW TO SET DYNAMIC HEIGHT OF SCOLLVIEW I.E. SOME VIEWS ARE CHANGING THERE HEIGHTS ACCORDING TO THERE CONTENTS
Let in the same above example, there are three views (aView, bView, cView) and there height is variable (as we dont know their height during designing, but we know minimum height let Ha, Hb, Hc respec.) and they are in view2.
Contriants of view2 is already given by us, now change its height relation from equalto(=)
to greaterthanequalto(>=)
.
Constraint of aView :
a) top to view2 : 0
b) leading to view2 : 0
c) trailing to view2 : 0
d) constant height : Ha, with relation greaterthanequalto(>=)
Constraint of bView :
a) top to aView : 0
b) leading to view2 : 0
c) trailing to view2 : 0
d) constant height : Hb, with relation greaterthanequalto(>=)
Constraint of cView :
a) top to bView : 0
b) leading to view2 : 0
c) trailing to view2 : 0
d) constant height : Hc, with relation greaterthanequalto(>=)
e) bottom to view2 : 0
NOTE Whenever you are dealing with scrollview you have to do the same, if your component is dynamic give height(keep minimun height) relation greaterthanequalto(>=)
and if your component have static height give relation equalto(=)