6
votes

Using Xcode 11 beta 7 and Catalina beta 19A536g (6?)

In my SwiftUI app I have a simple view with a list. Below the list I have a button to add items to the list.

After having added some items, the newly added items are not visible, since they are outside the content size of the list.

var body: some View {
  NavigationView {
    VStack {
      List(self.accounts) { account in
        Text("\(account.name)")
      }

      Button("Add new account") {
        self.addNewAccount()
      }
    }.navigationBarTitle("Select account")
  }
}

I guess I would like some binding the content offset of the list, passed to my method addNewAccount and trigger a scrolling of the list.

Can I somehow programmatically trigger the list to scroll down to the new content?

1
I remember this question's being asked before. A search on "[swiftui] list scroll" turned this up - since the answer wasn't accepted, it may be of help - and if it doesn't help, it may be that it's currently not possible: stackoverflow.com/questions/57258846/…dfd

1 Answers

0
votes

the easiest way to achieve it is to flip the list and its content using .scaleEffect(x: 1, y: -1, anchor: .center) in order to have an upside down structured list but with the correct looking content therefore, insertion of item at index 0 in the list will be performed at the bottom with a nice looking animation

List {
   ForEach(self.content) { itemContent in
       Item(itemContent).scaleEffect(x: 1, y: -1, anchor: .center)   
   }
}.scaleEffect(x: 1, y: -1, anchor: .center)