SwiftUI, How to scroll to the top?

swiftui scroll to bottom
scroll list to top swiftui
swiftui geometryreader in scrollview
swiftui scrollview
swiftui scrollview to top
swiftui uiscrollview
swiftui list scroll position
swiftui scrollview auto scroll to bottom

I am using ScrollView, and is that possible to scroll the view to the top when I do any actions, such as click button and etc?

Using this solution (previously simplified) I made example of moving ScrollView.content.offset strict to the top when button pressed:

struct ScrollToTheTop: View {

    @State private var verticalOffset: CGFloat = 0.0
    @State private var gestureOffset: CGFloat = 0.0
    @State private var itemCount: Int = 200

    var body: some View {

        NavigationView {
            VStack {

                ScrollView(.vertical, showsIndicators: false) {

                    VStack {

                        ForEach(1...itemCount, id: \.self) { item in
                            Text("--- \(item) ---") // height 17.5
                        }

                    }

                }
                .content.offset(y: (self.verticalOffset + self.gestureOffset)
                    // all the content divided by 2 for zero position of scroll view
                    + CGFloat(17.5 * Double(self.itemCount/2)))
                    .gesture(DragGesture().onChanged( { value in
                        self.gestureOffset = value.translation.height
                    }).onEnded( { value in
                        withAnimation {
                            // here should calculate end position with value.predictedEndLocation.y
                            self.verticalOffset += value.translation.height
                            self.gestureOffset = 0.0
                        }
                    }))


            }.navigationBarItems(trailing: Button("To top!") {
                withAnimation {
                    self.verticalOffset = 0.0
                }
            })
        }

    }

}

if this example fits, you have to refine DragGesture

Swiftui ScrollView reset position, I would like the that changes to data resets this scroll position, i.e. scroll to the top. I've included a small example of this behavior. Scroll down a bit and press the� In this tutorial, we will disclose a simple thing: How to scroll to the top of UITextView using Swift: Let’s say you have a ViewController and the UITextView variable named textView. Add self.textView.setContentOffset(.zero, animated: true) to viewDidAppear method in your ViewController

I think that currently the best way to do it is using the View´s id function

  1. Declare a state var with a unique id
    @State private var scrollViewID = UUID()
  1. and then add the id function to the ScrollView

    ScrollView(.horizontal, showsIndicators: true) {
    // ...
    }.id(self.scrollViewID)
    
    

Whenever you change that id - for example in a button event - the scrollview is rebuilt from scratch - which is like scrolling to the top

ScrollView effects using GeometryReader, When we use the frame(in:) method of a GeometryReader , SwiftUI in a vertical scroll view, each of which use the full width of the top-most� SwiftUI’s ScrollView allows us to create scrolling containers of views relatively easily, because it automatically sizes itself to fit the content we place inside it and also automatically adds extra insets to avoid the safe area. For example, we could create a scroll list of ten text views like this: ScrollView { VStack(spacing: 20) { ForEach(0..<10) { Text("Item \ ($0)") .foregroundColor(.white) .font(.largeTitle) .frame(width: 200, height: 200) .background(Color.red) } } }

There is a workaround that can accomplish ScrollToTop() effect by hiding everything before showing new content.

@State var hideEverything = false

var body: some View {
  ScrollView {
    if hideEverything {
      EmptyView()
    } else {
      // your content view
    }
  }
}

func ScrollToTop() {
  self.hideEverything = true
  DispatchQueue.main.asyncAfter(deadline: .now() + 0.01)  
  {  
    self.data = ... // update data source
    self.hideEverything = false
  }
}

How to make a scroll view move to a location using , If you want to programmatically make SwiftUI's ScrollView move to a specific location, you should embed a scrollTo(8, anchor: .top). Tip: If you� In this tutorial, you’ll learn what it takes to build scroll view in SwiftUI. You’ll learn: How to build scroll view from scratch. How to build a horizontal scroll view. How to build a vertical scroll view. Prerequisites. To follow along this tutorial, you’ll need some basic knowledge in: A basic familiarity with Swift. At least Xcode 11

In just published ScrollViewProxy.

A proxy value allowing the scrollable views within a view hierarchy to be scrolled programmatically. – https://developer.apple.com/documentation/swiftui/scrollviewproxy

Xcode 12.0 beta (12A6159)

SwiftUI Scroll Magic, public struct JumpingTitleBar: View { var scrollView: some View { ScrollView(. vertical) { Text(""" Scroll down to see the title bar jump from top to bottom. Scroll List to Row in SwiftUI. The List view in SwiftUI is very easy to use and provides a lot of functionality. We can quickly create rows and sections, add swipe to delete and implement editing. But, unfortunately, in the current iOS version 13.4.1 it's missing an API to control the scrolling.

How ScrollView lets us work with scrolling data – Moonshot SwiftUI , How ScrollView lets us work with scrolling data – Moonshot SwiftUI Tutorial 2/10. Paul Hudson Duration: 3:19 Posted: Oct 31, 2019 For the time being, SwiftUI does not seem to provide a native method to scroll programmatically. If you need such functionality, you may need to consider using UIViewRepresentable/UIViewControllerRepresentable.

SwiftUI Scroll Animation using GeometryReader, Detect your scroll position and apply 3D transform animations. My SwiftUI course Best Duration: 4:03 Posted: Jul 4, 2019 UITableView scroll to top with Swift. Scrolling to the top of a UITableView can be a useful feature depending on the type of app that you are making. If you have a tableview with hundreds of cells it can be annoying scrolling to the top of the tableview once you have scrolled far down the tableview. In this tutorial we will create a very basic UITableView and when we tap on a cell it will scroll to the top of the UITableView.

Writing a Custom Scroll View with SwiftUI in a chat application , It means that you would be stuck to displaying your chat window and the top and scroll down to see the new messages, which is not acceptable� Of course, you can also click and drag the scroll bar on the side of the page, but that's a slow and imprecise option--especially if you're using a laptop touchpad.

Comments
  • You can check my post on how to scroll to the bottom, just replace the code to make it scroll to the top. Here is the link stackoverflow.com/a/62719645/9497800