SwiftUI ScrollView not getting updated?

swiftui scrollview not working
swiftui scrollview paging
swiftui nested scrollview
swiftui scroll to item
swiftui scrollview safe area
swiftui list in scrollview
swiftui geometryreader in scrollview
swiftui list not updating

Goal

Get data to display in a scrollView

Expected Result

Actual Result

Alternative

use List, but it is not flexible (can't remove separators, can't have multiple columns)

Code

struct Object: Identifiable {
    var id: String
}

struct Test: View {
    @State var array = [Object]()

    var body: some View {
//        return VStack { // uncomment this to see that it works perfectly fine
        return ScrollView(.vertical) {
            ForEach(array) { o in
                Text(o.id)
            }
        }
        .onAppear(perform: {
            self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
        })
    }
}

A not so hacky way to get around this problem is to enclose the ScrollView in an IF statement that checks if the array is empty

if !self.array.isEmpty{
     ScrollView(.vertical) {
          ForEach(array) { o in
               Text(o.id)
          }
     }
}

SwiftUI don't update scrollview content via API, viewModel = viewModel } var body: some View { NavigationView { // 1 - This part not refreshing unless it is interacted with view then it refreshes. Not sure whether this is a SwiftUI bug or something we shouldn't use like this, but i had the same problem when trying to wrap UIPageViewController in a SwiftUIy way. – cargath Feb 19 at 9:47 Your custom function builder for arrays is great, though!

The expected behavior occurs on Xcode 11.1 but doesn't on Xcode 11.2.1

I added a frame modifier to the ScrollView which make the ScrollView appear

struct Object: Identifiable {
    var id: String
}

struct ContentView: View {
    @State var array = [Object]()

    var body: some View {
        ScrollView(.vertical) {
            ForEach(array) { o in
                Text(o.id)
            }
        }
        .frame(height: 40)
        .onAppear(perform: {
            self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
        })
    }
}

SwiftUI Vertical ScrollView not Updating Width when , 1 there is an issue with the SwiftUI ScrollView. It looks like the vertical ScrollView will be stuck with the initial width of its content, so if it's created with content of width zero, then it will stay with the width of zero points even when content changes, which results in no content being displayed. SwiftUI has just been updated and it has some great new features/views. One or two of them being LazyVGrid and LazyHGrid. So, in todays tutorial, we will be going through some of the basics of how to use these two new views. To start off with, what is LazyVGrid and LazyHGrid? According to Apple, it is a container view that arranges its child views into a grid. When using LazyVGrid the grid

I've found that it works (Xcode 11.2) as expected if state initialised with some value, not empty array. In this case updating works correctly and initial state have no effect.

struct TestScrollViewOnAppear: View {
    @State var array = [Object(id: "1")]

    var body: some View {
        ScrollView(.vertical) {
            ForEach(array) { o in
                Text(o.id)
            }
        }
        .onAppear(perform: {
            self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
        })
    }
}

ScrollView doesn't update its height when the height of a child view , ScrollView doesn't update its height when the height of a child view changes. I'd like to dynamically no comments yet. Be the first to share what you Done this with SwiftUI check out our YouTube channel - Kavsoft. View Comments. Play. The stated goal of SwiftUI is not “Write once, run anywhere” but “Learn once, use anywhere”. Don’t expect to be able to take a beautiful SwiftUI application for iOS, drop the code into a SwiftWebUI project and get it to render exactly the same in the browser. That is not the point.

One hacky workaround I've found is to add an "invisible" Rectangle inside the scrollView, with the width set to a value greater than the width of the data in the scrollView

struct Object: Identifiable {
    var id: String
}

struct ContentView: View {
    @State var array = [Object]()

    var body: some View {
        GeometryReader { geometry in
            ScrollView(.vertical) {
                Rectangle()
                    .frame(width: geometry.size.width, height: 0.01)
                ForEach(array) { o in
                    Text(o.id)
                }
            }
        }
        .onAppear(perform: {
            self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
        })
    }
}

Swiftui ScrollView reset position, I thought that the change you make to idx should force update. Visibly not. But see this: they change the state itself from a selector and seems to work. Should try� "SwiftUI, SwiftUI and SwiftUI" - SwiftUI's everywhere. In this way at every refresh just the newest data are. Not specific to SwiftUI, to display the array of landmarks we create a new model which is a subclass of MKAnnotation. 67-inch full-HD+ (1,080×2,340 pixels) curved AMOLED display with 90Hz refresh rate.

Borrowing from paulaysabellemedina, I came up with a little helper view that handles things the way I expected.

struct ScrollingCollection<Element: Identifiable, Content: View>: View {
    let items: [Element]
    let axis: Axis.Set
    let content: (Element) -> Content

    var body: some View {
        Group {
            if !self.items.isEmpty {
                ScrollView(axis) {
                    if axis == .horizontal {
                        HStack {
                            ForEach(self.items) { item in
                                self.content(item)
                            }
                        }
                    }
                    else {
                        VStack {
                            ForEach(self.items) { item in
                                self.content(item)
                            }
                        }
                    }
                }
            }
            else {
                EmptyView()
            }
        }
    }
}

How to embed List in ScrollView us…, How to embed List in ScrollView using SwiftUI The preview of this code shows the red borders of the ScrollView, but not showing anything for the list, nor the� The default ScrollView scrolls vertically, even if the direct child of the ScrollView is an HStack. This means that you have to use ScrollView(.horizontal) if you intend to override this behavior.

How do I get the content height of a ScrollView? – SwiftUI – Hacking , I'm pretty lost here as to how to properly do this. GeometryReaders inside a ScrollView seem to be very sketchy and not reliable. I tried using�

ScrollView SwiftUI Tutorials, Learn SwiftUI: ScrollView SwiftUI Tutorials and Other Resources Like Example Projects, At the time of writing, ScrollView in SwiftUI is not very customizable. This won't necessarily be best practices, but it should get you up and running.

ScrollView - Pull to Refresh, ScrollView in SwiftUI does not come with native support for refresh controls. will change the binding to true, when the view needs refreshing.

Comments
  • I tested this on Xcode 11.1, iOS 13.1, Swift 5 and am getting the expected result (although the scroll view is on top rather than center).
  • You also don't need the keyword return
  • interesting, I'm on Xcode 11.2, iOS 13.2, Swift 5
  • yeah I had a print statement before, hence the return
  • I'll update to Xcode 11.2 and see if I can reproduce this. It might take awhile but I'll update you on my findings.
  • Unfortunately not able to get it working on iOS 13.2 on Simulator
  • that works for the simple case, in my case I have a network call in onAppear, so initialising the scrollView with dummy data may be problematic
  • correct, but this situation can be handled logically in code, because initial object can be some easy-detected-stub to show conditionally something like 'loading' etc.. for me this was enough... so worth to share.