Currently, I have set up a UITableView
below a UIView
whose origin is (0, 287) in an example of an iPhone 7.
This is achieved using the following sample code:
extension ViewController: UIScrollViewDelegate
{
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
if scrollView.contentOffset.y > 0 && imageContainerViewHeightConstraint.constant == 223
{
return
}
if scrollView.contentOffset.y > 0
{
var sd = imageContainerViewHeightConstraint.constant + abs(scrollView.contentOffset.y)
if (sd < 223 )
{
print("path111 1")
self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0 )
return
}
else
{
imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
}
view.layoutIfNeeded()
self.dataTableView.contentOffset = .zero
return
}
if scrollView.contentOffset.y < 0 && imageContainerViewHeightConstraint.constant >= initialContainerImageViewHeight * 2
{
self.dataTableView.contentOffset = .zero
return
}
else
{
imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
view.layoutIfNeeded()
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
resetContainerViewSize()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
resetContainerViewSize()
}
}
func resetContainerViewSize()
{
imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight
UIView.animate(withDuration: 0.4,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: .curveEaseInOut,
animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
If I slowly scroll down, then slowly scroll up, the UITableView
will stop at the origin and go no further up, and never enters the condition if (sd < 223 )
....like so:
However, if I scroll down, and pull up fast, once the top of the section header reaches the initial origin of (0, 287), the UITableView
will scroll a bit further up and enters the condition if (sd < 223 )
, because once I let go, the UITableView
will bounce back down to its origin.....like so:
If I pull up on the UITableView
even faster, it will scroll even further up and bounce back down once I let go of the UITableView
:
I've found some similar questions:
- Stop UITableView over scroll at top & bottom?
- Limit the scroll for UITableView
- Prevent UITableView scrolling below a certain point
I've tried the suggested solution to a variation, by disabling alwaysBounceVertical
and bounces
, but this just disables scrolling altogether, which is not what I want.
Since UITableView
is a subclass of UIScrollView
, I've always tried setting scrollView.contentOffset = .zero
within the condition if(sd < 223 )
, which also did not help.
How would I prevent the UITableView
from being pulled up past its initial origin points, i.e. (0, 287)?
I've included a sample project: sample project