Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift

scrollview doesn't scroll swift
uiscrollview not scrolling swift autolayout
vertical scrollview swift
scroll view (swift)
uiscrollview subclass swift
scrollview scroll to position swift
swift uiscrollview programmatically
scrollable view controller swift

I have a few screens worth of content within my UIScrollView which only scrolls vertically.

I want to programmatically scroll to a view contained somewhere in it's hierarchy.

The UIScrollView move so that the child view is at the top of the UIScrollView (either animated or not)

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)
        }
    }

}

Scrollview scroll to top programmatically swift, Scrollview scroll to top programmatically swift. All other subviews have a left constraint to the right of the previous If the user tries to scroll 'beyond' the 4th  Same-directional scrolling occurs when a UIScrollView that is a subview of a UIScrollView both scroll in the same direction. You can use this to add a section in your main scroll view’s view that contains additional separate data.

 scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height:
1), animated: true)

or

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

Another way is

scrollView.contentOffset = CGPointMake(x,y);

and i do it with animated like this

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
scrollView.contentOffset = CGPointMake(x, y); }
completion:NULL];

Scroll View Guide, The UITableView and UICollectionView are a subclasses of UIScrollView , so Each subview has the same width as the scroll view, but we offset the top of currentViewOffset/contentHeight let subview = UIView(frame: frame) subview. Unlike other views, you cannot rely on the size of child views of the scroll view here. In this video I demonstrate how to programmatically add several images as subviews to UIScrollView in Swift. In my example I use UIImageView but it can be a custom user interface design ex. XIB file. To learn how to create your own reusable custom user interface files watch this video: Creating custom user interface files…

scrollView.setContentOffset(CGPoint, animated: Bool)

Where the point's y coordinate is the y coordinate of the frame of the view you want to show relatively to the scrollView's content view.

[iOS] How to Solve UIScrollView Constraint Ambiguity, Scroll View has ambiguous scrollable content width on a 150 x 150 subview where its leading/trailing/top/bottom must align with its 200 x 200  Setting the content offset to the height of the content size is wrong: it scrolls the bottom of the content to the top of the scroll view, and thus out of sight. The correct solution is to scroll the bottom of the content to the bottom of the scroll view, like this (sv is the UIScrollView): @jeffamaphone - Thanks for asking.

Here is my answer, this is in swift. This will scroll the pages in scrollview infinitely.

private func startBannerSlideShow()
{
UIView.animate(withDuration: 6, delay: 0.1, options: .allowUserInteraction, animations: {
    scrollviewOutlt.contentOffset.x = (scrollviewOutlt.contentOffset.x == scrollviewOutlt.bounds.width*2) ? 0 : scrollviewOutlt.contentOffset.x+scrollviewOutlt.bounds.width
}, completion: { (status) in
    self.startBannerSlideShow()
})
}

Using UIScrollView with Auto Layout in iOS, In interface builder, drag a UIScrollView and add it as a sub view of the view Using a single child view will simplify the constraints we have to add later. Contain content of UIscrollView in one UIView. Our next Attach four constraints (top, bottom, left, right) from our single content view to the scroll view. i have a scrollview and am trying to scroll to its bottom programmatically.. tried these: extension UIScrollView { // Bonus: Scroll to bottom func scrollToBottom() { let bottomOffset = CGPoi

UIScrollView Tutorial: Getting Started, Update note: Ron Kliffer updated this tutorial for Xcode 11, Swift 5 and iOS 13. Use UIScrollView for vertical scrolling with Auto Layout. Add four new constraints: top, bottom, leading and trailing. The scroll view calls this method to determine which of its subviews to scale when the user pinches the  We iterate over the subviews // 2, add them to the UIScrollView // 3 and add the Auto Layout constraints following this 4 simple rules: 1. All subviews have a top constraint of 0 and a size constraint that is equal to the scroll view. // 4 2. The first subview (in other words the leftmost subview) has a left constraint of 0 // 5 3.

touchesShouldCancel(in:), If it receives false from this method, it stops dragging and forwards the touch events to the content subview. The scroll view does not call this method if the value  The UIStack View is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. Instead, it just manages the position and size of its arranged views.

How to prevent UIScrollView subviews from scrolling the scrollView , So I have programmatically added a scrollView and a bunch of subviews to it. Let me show you: subview. scrollView.addSubview(imageView) // This is a parent array for all the UIViews added. Swift 5.3 will bring official Windows support. Add top and bottom constraints from Scroll view to the Top and Bottom Layout guides, respectively. They should also have constants of 0. Add a view as a child of the Scroll view and resize it to fit the entire space of the Scroll view. Rename its storyboard Label to Container View.

UIScrollView Class (UIKit), iOS.dll. A UIView that can scroll, pan, and page its children. Add a single large subview, generally with a Frame whose System.Drawing. If true , then the scroll view will jump to the top of the content when the user taps on the status bar. TL; DR; The code to support UIScrollView with autolayout is open-sourced and hosted on Github. The wrapper can be found here and example on how to use it is listed here Recently I read an article about using UIScrollView with Autolayout on iOS platform.

Comments
  • This is not working well for me if the keyboard is on screen. Instead of using scrollRectToVisible i'm using: setContentOffset(CGPoint(x: 0, y: childStartPoint.y), animated: animated)
  • self.scrollRectToVisible(CGRect(x: 0, y: childStartPoint.y, width: 1, height: view.frame.height), animated: animated) worked for me. Used view height
  • Awesome Answer :)
  • consider replacing height: self.frame.height with height: frame.height - (contentInset.top + contentInset.bottom) when using content insets.
  • This does not answer the question