1

I have the below code.

The weird part: This method works perfectly as intended... as long as I nudge the scroll. It needs only what seems to be a pixel width scroll and then proceeds to work perfectly.

I get the feeling this is a swiftui bug or maybe i'm doing it a dumb way.

Thanks for the help.

ScrollViewReader { scrollProxy in
    ScrollView(.horizontal, showsIndicators: false) {
        VStack {
            Text(text)
                .id(0)
                .foregroundColor(.white)
                .font(.system(size: fontSize , design: .rounded))
            Spacer()
        }
    }
    .scrollBounceBehavior(.basedOnSize, axes: .horizontal)
    .onChange(of: text) { _ in
        scrollProxy.scrollTo(0, anchor: .trailing)
    }
    .padding([.horizontal, .top])
}

P.S. Removing the VStack/Spacer makes no difference to the issue. Same with scollBounceBehavior.

  • What do you mean by "a pixel width scroll" and "nudge the scroll"? It works exactly as how I expected it to work. – Sweeper Jul 04 '23 at 03:49
  • I update the text and it does not move and/or scrollTo the end.. until i scroll manually, after that, updating the text makes it scroll to the end for some reason – Chris de Wet Jul 04 '23 at 04:34
  • How are you updating the text? What was the actual text before and after the update? – Sweeper Jul 04 '23 at 04:51
  • E.g. Before: "abc", after: "abcd". I'm updating it via a function. I've done this in a seperate test and just used a button that appends a character to the end of a string and it still has the same issue. The variable is a state variable also. – Chris de Wet Jul 04 '23 at 07:29

1 Answers1

0

I found it!

The text needs to be placed in a foreach like so:

ForEach(Array(text.enumerated()), id: \.offset) { i, v in

but then, the id is not updated before the onchange proxy scoll is called so it does not scroll. I am trying to think of a better way to solve this but at least I know why it's not working.