Check if a UIScrollView reached the top or bottom
Implement the UIScrollViewDelegate
in your class, and then add this:
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset == 0)
{
// then we are at the top
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
// then we are at the end
}
}
Hope this is what you are after! Else have a tinker by adding more conditions to the above code and NSLog the value of scrollOffset.
Determine if view reached the top of the UIScrollView
Just convert the top of the scroll view and the top of v3 to the same coordinate system and now you can simply look to see which one is higher.
Let sv
be the scroll view and v3
be view 3. Then:
func scrollViewDidScroll(_ sv: UIScrollView) {
let svtop = sv.frame.origin.y
let v3top = sv.superview!.convert(v3.bounds.origin, from:v3).y
if v3top < svtop { print("now") }
}
Detect UIScrollview bottom reached
Have you thought of adding a boolean. update it when the method is called for the first time and maybe when user scrolls back up.
UIScrollView, reaching the bottom of the scroll view
I think what you might be able to do is to check that your contentOffset
point is at the bottom of contentSize
. So you could probably do something like:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height) {
// we are at the end
}
}
You'll likely also need a negative case there to show your indicator when the user scrolls back up. You might also want to add some padding to that so, for example, you could hide the indicator when the user is near the bottom, but not exactly at the bottom.
How to determine that the scroll reached to specific position(I have to determine the space from bottom should be 56 px) while scrolling in iOS swift
You need to calculate the bottomOffset
first, then calculate the difference between it and the contentOffset
. If it's <= 56, then you reached a specific position.
let bottomOffset = scrollView.contentSize.height - scrollView.frame.size.height
let position = bottomOffset - scrollView.contentOffset.y
if position <= 56 {
print(" you reached at desired bottom")
self.showPageControl(toShow: true)
} else {
self.showPageControl(toShow: false)
}
How can I detect when iOS ScrollView is at the very top?
scrollViewDidEndDragging
means the user's finger lifted, but because it has physics to simulate inertia, the view may continue to move. There is another delegate method for exactly what you are looking for: scrollViewDidScrollToTop which as the name implies fires when you get to the top of the scrollview.
EDIT:
you can also try:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
print("top!")
}
}
EDIT third time's the charm? This one only fires once, after the scroll view has stopped moving, if you are at the top.
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y == 0 {
print("top!")
}
}
How to check if a scroll view scrolled in bottom or not in ios
implement UIScrollViewDelegate
in the class that hosts UIScrollView.
set the scrollView.delegate
property.
implement below method.
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
//This condition will be true when scrollview will reach to bottom
}
}
How do I detect when User has reached the bottom of the ScrollView?
Wrap your whole ScrollView
in your ChildSizeReader
, so you can get the height of the ScrollView
itself.
Because the offset starts at zero at the top, when at the bottom of the scroll view the end isn't at the top of the screen, but rather the bottom. This difference is the height of the scroll view. This means the ScrollView
starts at offset 0
and goes to total content height - scroll view height
.
Code:
struct ContentView: View {
let spaceName = "scroll"
@State var wholeSize: CGSize = .zero
@State var scrollViewSize: CGSize = .zero
var body: some View {
ChildSizeReader(size: $wholeSize) {
ScrollView {
ChildSizeReader(size: $scrollViewSize) {
VStack {
ForEach(0..<100) { i in
Text("\(i)")
}
}
.background(
GeometryReader { proxy in
Color.clear.preference(
key: ViewOffsetKey.self,
value: -1 * proxy.frame(in: .named(spaceName)).origin.y
)
}
)
.onPreferenceChange(
ViewOffsetKey.self,
perform: { value in
print("offset: \(value)") // offset: 1270.3333333333333 when User has reached the bottom
print("height: \(scrollViewSize.height)") // height: 2033.3333333333333
if value >= scrollViewSize.height - wholeSize.height {
print("User has reached the bottom of the ScrollView.")
} else {
print("not reached.")
}
}
)
}
}
.coordinateSpace(name: spaceName)
}
.onChange(
of: scrollViewSize,
perform: { value in
print(value)
}
)
}
}
Note your already existing scrollViewSize
variable is the content's size, not the scroll view's size.
Also notice that I changed the ==
to >=
- this is so you don't have to be exactly at the height, can be over-scrolled where it rubber-bands back.
Related Topics
In iPhone App How to Detect the Screen Resolution of the Device
Why Do We Use Use_Frameworks! in Cocoapods
How to Use Iboutletcollection to Connect Multiple Uiimageviews to the Same Outlet
Change Tab Bar Item Selected Color in a Storyboard
Logging Data on Device and Retrieving the Log
No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development
Google Plus API for Posting on Wall Like Facebook
How to Draw a Transparent Uitoolbar or Uinavigationbar in iOS7
How to Check If Uilabel Is Truncated
How to Implement the Hashable Protocol in Swift for an Int Array (A Custom String Struct)
Xcode Nsmanagedobject Subclass Contains Optionals When They Are Marked as Non-Optional
How to Get the Console Logs from the iOS Simulator
How to Reset the iOS Simulator from the Command Line
How to Create Layout Constraints Programmatically
Uicollectionview: Must Be Initialized with a Non-Nil Layout Parameter