Get UIScrollView to scroll to the top

Get UIScrollView to scroll to the top

uiscrollview scroll to top programmatically swift
uitableview scroll to top swift 4
uiscrollview scroll to bottom
swiftui scrollview scroll to top
scrollview scroll to top react-native
tap status bar to scroll to top ios
uicollectionview scroll to top
uiscrollview setcontentoffset

How do I make a UIScrollView 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 , extension UIScrollView { // Scroll to a specific view so that it's top is at the animated: Bool) { if let origin = view.superview { // Get the Y position  The scroll-to-top gesture is a tap on the status bar. When a user makes this gesture, the system asks the scroll view closest to the status bar to scroll to the top. If that scroll view has scrolls To Top set to false, its delegate returns false from scroll View Should Scroll To Top(_:), or the content is already at the top, nothing happens.


Here is a Swift extension that makes it easy:

extension UIScrollView {
    func scrollToTop() {
        let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
        setContentOffset(desiredOffset, animated: true)
   }
}

Usage:

myScrollView.scrollToTop()

Manually scrolling UIScrollView (iOS/Swift), Top, Right, Bottom, Left). It took me quite time to find examples online. But I take this moment to compile them together. Since Swift is a hot word in  This means if you call this, it doesn't actually scroll all the way to the top. Aside from preserving the original offset in memory from when the UIScrollView loads, is there a way to recover this value later to ensure that it does indeed scroll to top consistently, no matter the " adjustedContentInset "?


iOS 11 and above

try to work with the new adjustedContentInset.

For example (scroll to the top):

var offset = CGPoint(
    x: -scrollView.contentInset.left, 
    y: -scrollView.contentInset.top)

if #available(iOS 11.0, *) {
    offset = CGPoint(
        x: -scrollView.adjustedContentInset.left, 
        y: -scrollView.adjustedContentInset.top)    
}

scrollView.setContentOffset(offset, animated: true)

Even if you use prefersLargeTitles, safe area etc.

scrollsToTop, A Boolean value that controls whether the scroll-to-top gesture is enabled. SDKs. iOS 2.0+; Mac Catalyst 13.0+ Declaration. var scrollsToTop: Bool { get set }  To do slideshows with UIScrollView, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated: when the timer fires. But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See iPhone Image slideshow for details.


For Swift 4

scrollView.setContentOffset(.zero, animated: true)

scrollViewDidScrollToTop:, Tells the delegate that the scroll view scrolled to the top of the content. bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES. But if anyone else is as crazy as me - and your scrollview is at the bottom by default - this will scroll it to the top: [scrollView setContentOffset:CGPointMake(0, scrollView.contentSize.height-scrollView.frame.size.height) animated:YES]; – GilesDMiddleton Apr 1 '15 at 16:03


Use setContentOffset:animated:

[scrollView setContentOffset:CGPointZero animated:YES];

Get UIScrollView to scroll to the top, contentInset.top) animated:YES]; ORIGINAL [self.scrollView setContentOffset:​CGPointZero animated:YES]; or if you want to preserve the horizontal scroll  Now let’s get started building the UI. In interface builder, drag a UIScrollView and add it as a sub view of the view controller’s main view. Now add some constraints to your scroll view to place it where you want it in your main view. In my example, the scroll view takes up the whole view,


A little Story about UIScrollView in iOS - Swift India, UIScrollView is an old and versatile class in UIKit, and has been around since iOS Asks the delegate if the scroll view should scroll to the top of the content. Hi, How to scroll to top of the page automatically while clicking on the button? Thanks. This site uses cookies for analytics, personalized content and ads. By


Understanding Scroll Views · objc.io, Thus, most of the understanding of how a UIScrollView works comes from Then​, that composited image is composited on top of the super-superview's image, and We also have another article in this issue explaining in detail how pixels get  Xcode is complaining that your Auto Layout rules are wrong. To fix them, select Scroll View and click the Pin button at the bottom of the storyboard window. Add four new constraints: top, bottom, leading and trailing. Set each constraint’s constant to 0 and uncheck Constrain to margins.


Get UIScrollView to scroll to the top, In iOS7 I had trouble getting a particular scrollview to go to the top, whihc worked in iOS6, and used this to set the scrollview to go to the top. …is a sure bet to scroll the window (or any other element) back to the top. The scrolling behavior of that is determined by CSS as well, but if you aren’t doing that, you can force the smoothness in JavaScript: window.scroll({ top: 0, left: 0, behavior: 'smooth' });