Sunday, July 14, 2024
HomeiOS Developmentios - SwiftUI preserving monitor of and sustaining scroll place on machine...

ios – SwiftUI preserving monitor of and sustaining scroll place on machine rotation


I’ve a ScrollView containing a ForEach iterating by means of an array of parts of sort Verse. Every view throughout the ForEach is a VStack with some Arabic textual content and a few English textual content.

The English textual content is barely seen when readingMode is false. The Arabic textual content becoms aligned to the centre when readingMode is false

There are 3 elements to the issue:

  1. When the machine is rotated, the view doesn’t keep the scroll place
  2. When the view seems, there could also be an preliminary scroll place (a verse which the view ought to begin at)
  3. I need to have the ability to replace the scroll place primarily based on a particular verse inside a wheel picker

Here’s a simplified instance of the code I’ve that comprises solely the important elements of the view. Please let me know in case you want any extra info.

ScrollView {
    ForEach(verses) { verse in
        VStack {
            HStack {
                if !readingMode {
                    Spacer()
                }
                
                Textual content("Some Arabic Textual content")
            }
            
            if !readingMode {
                Textual content("Some English Textual content")
            }
        }.id(verse.id)
    }
}

I’ve tried utilizing modifiers comparable to .onChange together with UIDevice.present.orientation and the .scrollPosition modifer to get the scroll place earlier than rotation and set that because the scroll place after rotation however that is very buggy and sometimes would not work

I’ve managed to attain half 2 and three of the issue utilizing a ScrollViewReader and the scrollTo operate however the ScrollView can, consequently, randomly bounce to a unique a part of the view for seemingly no cause.

From what I’ve discovered on-line by means of many hours of analysis, SwiftUI ought to routinely keep the scroll place when the machine is rotated so I may be doing one thing fully mistaken that’s inflicting all my issues.

Thanks upfront on your assist!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments