Get UIScrollView to scroll to the top
UPDATE FOR iOS 7
[self.scrollView setContentOffset:
CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];
ORIGINAL
[self.scrollView setContentOffset:CGPointZero animated:YES];
or if you want to preserve the horizontal scroll position and just reset the vertical position:
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0)
animated:YES];
Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift
Here's an extension I ended up writing.
Usage:
Called from my viewController, self.scrollView is an outlet to the UIScrollView and self.commentsHeader is a view within it, near the bottom:
self.scrollView.scrollToView(self.commentsHeader, animated: true)
Code:
You only need the scrollToView method, but leaving in scrollToBottom / scrollToTop methods too as you'll probably need those too, but feel free to delete them.
extension UIScrollView {
// Scroll to a specific view so that it's top is at the top our scrollview
func scrollToView(view:UIView, animated: Bool) {
if let origin = view.superview {
// Get the Y position of your child view
let childStartPoint = origin.convertPoint(view.frame.origin, toView: self)
// Scroll to a rectangle starting at the Y of your subview, with a height of the scrollview
self.scrollRectToVisible(CGRect(x:0, y:childStartPoint.y,width: 1,height: self.frame.height), animated: animated)
}
}
// Bonus: Scroll to top
func scrollToTop(animated: Bool) {
let topOffset = CGPoint(x: 0, y: -contentInset.top)
setContentOffset(topOffset, animated: animated)
}
// Bonus: Scroll to bottom
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
if(bottomOffset.y > 0) {
setContentOffset(bottomOffset, animated: true)
}
}
}
How to use UIScrollView to scroll to the top of the first view?
This may help.
scrollView.contentInsetAdjustmentBehavior = .never
For more information,
This property specifies how the safe area insets are used to modify the content area of the scroll view. The default value of this property is UIScrollViewContentInsetAdjustmentAutomatic.
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") }
}
UIScrollView scroll to bottom programmatically
You can use the UIScrollView's setContentOffset:animated:
function to scroll to any part of the content view. Here's some code that would scroll to the bottom, assuming your scrollView is self.scrollView
:
Objective-C:
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];
Swift:
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)
Hope that helps!
How to get UIScrollView vertical direction in Swift?
If you use an UIScrollView
then you can take benefit from the scrollViewDidScroll:
function. You need to save the last position (the contentOffset
) it have and the update it like in the following way:
// variable to save the last position visited, default to zero
private var lastContentOffset: CGFloat = 0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset > scrollView.contentOffset.y) {
// move up
}
else if (self.lastContentOffset < scrollView.contentOffset.y) {
// move down
}
// update the new position acquired
self.lastContentOffset = scrollView.contentOffset.y
print(lastContentOffset)
}
There are other ways of doing it of course this is one of them.
I hope this helps you.
UIScrollView scroll to button when keyboard is visible
Did you try it the following way?
func keyboardWillShow(notification: Notification) {
self.scrollView.isScrollEnabled = true
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var rect = self.view.frame
rect.size.height -= self.keyboardSize.height
if !rect.contains(self.loginButton.frame.origin) {
self.scrollView.scrollRectToVisible(self.loginButton.frame, animated: true)
}
}
After hours of trying this is the way I do the scrolling in my applications.
Finding the direction of scrolling in a UIScrollView?
Determining the direction is fairly straightforward, but keep in mind that the direction can change several times over the course of a gesture. For example, if you have a scroll view with paging turned on and the user swipes to go to the next page, the initial direction could be rightward, but if you have bounce turned on, it will briefly be going in no direction at all and then briefly be going leftward.
To determine the direction, you'll need to use the UIScrollView scrollViewDidScroll
delegate. In this sample, I created a variable named lastContentOffset
which I use to compare the current content offset with the previous one. If it's greater, then the scrollView is scrolling right. If it's less then the scrollView is scrolling left:
// somewhere in the private class extension
@property (nonatomic, assign) CGFloat lastContentOffset;
// somewhere in the class implementation
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
ScrollDirection scrollDirection;
if (self.lastContentOffset > scrollView.contentOffset.x) {
scrollDirection = ScrollDirectionRight;
} else if (self.lastContentOffset < scrollView.contentOffset.x) {
scrollDirection = ScrollDirectionLeft;
}
self.lastContentOffset = scrollView.contentOffset.x;
// do whatever you need to with scrollDirection here.
}
I'm using the following enum to define direction. Setting the first value to ScrollDirectionNone has the added benefit of making that direction the default when initializing variables:
typedef NS_ENUM(NSInteger, ScrollDirection) {
ScrollDirectionNone,
ScrollDirectionRight,
ScrollDirectionLeft,
ScrollDirectionUp,
ScrollDirectionDown,
ScrollDirectionCrazy,
};
scrolling a scrollview to its bottom programmatically
The offset setting doesn't works because you tried in calling early in the life cycle.
You could try updating the contentOffset
at viewDidLayoutSubviews
or viewDidAppear
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)
Related Topics
Using Tesseract to Recognize License Plates
Uicollectionview with a Sticky Header
Using iOS Gamekit's "Bluetooth Bonjour" with Other Platforms
How to Scroll to the Exact End of the Uitableview
Disable the Uitableview Highlighting But Allow the Selection of Individual Cells
Xcode Info.Plist Build Variable ${Product_Name:Rfc1034Identifier} Seems Completely Undocumented
Populating Tableview with Multiple Sections and Multiple Dictionary in an Array in Swift
iOS - Detect Blow into Mic and Convert the Results! (Swift)
How to Make Cmake Use Specific Compiler and Flags When Final Compilation Stage Instead of Detection
The Paper Folding/Unfolding Effect in Twitter for iPad
Frosted Glass (iOS 7 Blur) Effect
How to Solve "Error Running Pod Install" in Flutter on MAC
Xcode 8, iOS 10 - "Starting Webfilter Logging for Process"