UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: ScrollView - push contents to top

Forums > SwiftUI

I'm trying to display the contents of a text file in a ScrollView that has both vertical and horizontal scrolling.

All is well when then contents of the file are large enough to fill the screen. However, for smaller files e.g. only 3 lines, the text is displayed, but in the middle of the screen. I've tried adding a Spacer() in various places but this does not help. Also, removing the horizontal scrolling pushes the contents to the top, but I need both vertical and horizontal scrolling.

Here's some example code showing the problem:

struct ContentView: View {
    var string = """
    Line 1 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Line 2 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Line 3 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    """

    var body: some View {
        NavigationView {
            ScrollView([.horizontal, .vertical]) {
                    Text(string)
                Spacer()
            }
            .padding()
            .navigationTitle("Scrolling")
        }
    }
}

Here's a link to a screenshot of the problem - screenshot

Does anyone know how to push the contents of the ScrollView to the top of the screen?

Thanks.

Gavin

4      

This works for me. Not sure if this is the optimal solution, though.

Add a second ScrollView().

struct ScrollViewTroubleShooting: View {
    var string = """
    Line 1 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Line 2 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Line 3 - Lorem ipsum dolor sit amet, consectetur adipiscing elit
    """

    var body: some View {
        NavigationView {
            VStack {
                Text("Label")
                ScrollView([.horizontal]) { // First scroll view is horizontal
                    ScrollView([.vertical]) { // Second scroll view is vertical
                        Text(string)
                    }
                    .padding()
                    .navigationTitle("Scrolling")
                }
            }
        }
    }
}

3      

@Obelix - interesting solution! Thanks.

3      

I'm getting the same result as you @gavinjerman when using ScrollView with two simultaneous axis. The content offset defaults to the top of the contained view when using one axis (either one) but it gets scrolled to the center of the contained view when adding the second axis to the array of axis.

I'm trying to figure out why this happens (and how to control it) but it seems hard to find. Any ideas?

The workaround by @Obelix is an option until I try implementing a magnification gesture to take care of the zoom, then I need both axis in the ScrollView.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.