SwiftUI List animation when changing data source - swiftui

I have a List in my SwiftUI app, with on top buttons to see the previous/next category of items to be loaded into the list. When you press the buttons, the source of the List changes, and the List updates with its default animation (where rows are folding away).
Code for a simplified reproducible version (see below for a screenshot):
import SwiftUI
struct ContentView: View {
private let models = [
["a", "b", "c", "d", "e", "f"],
["g", "h"],
["i", "j", "k", "l"],
]
#State private var selectedCategory = 1
private var viewingModels: [String] {
models[selectedCategory]
}
var body: some View {
VStack(spacing: 0.0) {
HStack {
Button(action: previous) {
Text("<")
}
Text("\(selectedCategory)")
Button(action: next) {
Text(">")
}
}
List(viewingModels, id: \.self) { model in
Text(model)
}
}
}
private func previous() {
if selectedCategory > 0 {
selectedCategory -= 1
}
}
private func next() {
if selectedCategory < (models.count - 1) {
selectedCategory += 1
}
}
}
Now, I don't want to use the default List animation here. I want the list items to slide horizontally. So when you press the > arrow to view the next category of items, the existing items on screen should move to the left, and the new items should come in from the right. And the reverse when you press the < button. Basically it should feel like a collection view with multiple pages that you scroll between.
I already found that wrapping the contents of the previous and next functions with withAnimation changes the default List animation to something else. I then tried adding .transition(.slide) to the List (and to the Text within it) to change the new animation, but that doesn't have an effect. Not sure how to change the animation of the List, especially with a different one/direction for the 2 different buttons.
Using a ScrollView with a List per category is not going to scale in the real app, even though yea that might be a solution for this simple example with very few rows :)

If your button is wrapped as you suggested and I added a simple direction boolean:
Button(action: {
withAnimation {
slideRight = true
self.previous()
}
}) {
Text("<")
}
And opposite for the other direction:
Button(action: {
withAnimation {
slideRight = false
self.next()
}
}) {
Text(">")
}
Then you can transition your view like this:
List(viewingModels, id: \.self) { model in
Text(model)
}
.id(UUID())
.transition(.asymmetric(insertion: .move(edge: slideRight ? .leading : .trailing),
removal: .move(edge: slideRight ? .trailing : .leading)))
Note that in order for the list to not animate, we need to give the list a new unique ID each time, see this article: https://swiftui-lab.com/swiftui-id/
UPDATE:
I wanted to provide the full shortened code that works, also removed the UUID() usage based on comments below.
import SwiftUI
struct ContentView: View {
private let models = [
["a", "b", "c", "d", "e", "f"],
["g", "h"],
["i", "j", "k", "l"],
]
#State private var selectedCategory = 0
#State private var slideRight = true
private var viewingModels: [String] {
models[selectedCategory]
}
var body: some View {
VStack(spacing: 0.0) {
HStack {
Button(action: {
withAnimation {
if(self.selectedCategory - 1 < 0) { self.selectedCategory = self.models.count - 1 }
else { self.selectedCategory -= 1 }
self.slideRight = true
}
}) {
Image(systemName: "arrow.left")
}
Text("\(selectedCategory + 1)")
Button(action: {
withAnimation {
if(self.selectedCategory + 1 > self.models.count - 1) { self.selectedCategory = 0 }
else { self.selectedCategory += 1 }
self.slideRight = false
}
}) {
Image(systemName: "arrow.right")
}
}.font(.title)
List(viewingModels, id: \.self) { model in
Text(model)
}
.id(selectedCategory)
.transition(.asymmetric(insertion: .move(edge: slideRight ? .leading : .trailing),
removal: .move(edge: slideRight ? .trailing : .leading)))
}.padding(10)
}
}

Related

how to make the recursive view the same width?

I want to make a recursive view like this:
But what I have done is like this:
It's a tvOS application, the sample code is:
struct MainView: View {
#State private var selectedItem: ListItem?
var body: some View {
VStack {
RecursiveFolderListView(fileId: "root", selectedItem: $selectedItem)
}
}
}
struct RecursiveFolderListView: View {
#EnvironmentObject var api: API
var fileId: String
#Binding var selectedItem: ListItem?
#State private var currentPageSelectedItem: ListItem?
#State private var list: [ListItem]?
#State private var theId = 0
var body: some View {
HStack {
if let list = list, list.count > 0 {
ScrollView(.vertical) {
ForEach(list, id: \.self) { item in
Button {
selectedItem = item
currentPageSelectedItem = item
} label: {
HStack {
Text(item.name)
.font(.callout)
.multilineTextAlignment(.center)
.lineLimit(1)
Spacer()
if item.fileId == selectedItem?.fileId {
Image(systemName: "checkmark.circle.fill")
.resizable()
.scaledToFit()
.frame(width: 30, height: 30)
.foregroundColor(.green)
}
}
.frame(height: 60)
}
}
}
.focusSection()
.onChange(of: currentPageSelectedItem) { newValue in
if list.contains(where: { $0 == newValue }) {
theId += 1
}
}
} else {
HStack {
Spacer()
Text("Empty")
Spacer()
}
}
if let item = currentPageSelectedItem, item.fileId != fileId {
RecursiveFolderListView(fileId: item.fileId, selectedItem: $selectedItem)
.id(theId)
}
}
.task {
list = try? await api.getFiles(parentId: fileId)
}
}
}
It's a list view, and when the user clicks one item in the list, it will expand the next folder list to the right. The expanded lists and the left one will have the same width.
I think it needs Geometryreader to get the full width, and pass down to the recursive hierarchy, but how to get how many views in the recursive logic?
I know why my code have this behavior, but I don't know how to adjust my code, to make the recursive views the same width.
Since you didn't include definitions of ListItem or API in your post, here are some simple definitions:
struct ListItem: Hashable {
let fileId: String
var name: String
}
class API: ObservableObject {
func getFiles(parentId: String) async throws -> [ListItem]? {
return try FileManager.default
.contentsOfDirectory(atPath: parentId)
.sorted()
.map { name in
ListItem(
fileId: (parentId as NSString).appendingPathComponent(name),
name: name
)
}
}
}
With those definitions (and changing the root fileId from "root" to "/"), we have a simple filesystem browser.
Now on to your question. Since you want each column to be the same width, you should put all the columns into a single HStack. Since you use recursion to visit the columns, you might think that's not possible, but I will demonstrate that it is possible. In fact, it requires just three simple changes:
Change VStack in MainView to HStack.
Change the outer HStack in RecursiveFolderListView to Group.
Move the .task modifier to the inner HStack around the "Empty" text, in the else branch.
The resulting code (with unchanged chunks omitted):
struct MainView: View {
#State private var selectedItem: ListItem? = nil
var body: some View {
HStack { // ⬅️ changed
RecursiveFolderListView(fileId: "/", selectedItem: $selectedItem)
}
}
}
struct RecursiveFolderListView: View {
...
var body: some View {
Group { // ⬅️ changed
if let list = list, list.count > 0 {
...
} else {
HStack {
Spacer()
Text("Empty")
Spacer()
}
.task { // ⬅️ moved to here
list = try? await api.getFiles(parentId: fileId)
}
}
}
// ⬅️ .task moved from here
}
}
I don't have the tvOS SDK installed, so I tested by commenting out the use of .focusSection() and running in an iPhone simulator:
This works because the subviews of a Group are “flattened” into the Group's parent container. So when SwiftUI sees a hierarchy like this:
HStack
Group
ScrollView (first column)
Group
ScrollView (second column)
Group
ScrollView (third column)
HStack (fourth column, "Empty")
SwiftUI flattens it into this:
HStack
ScrollView (first column)
ScrollView (second column)
ScrollView (third column)
HStack (fourth column, "Empty")
I moved the .task modifier because otherwise it would be attached to the Group, which would pass it on to all of its child views, but we only need the task applied to one child view.
Although rob's answer is perfect, I want to share another approach.
class SaveToPageViewModel: ObservableObject {
#Published var fileIds = [String]()
func tryInsert(fileId: String, parentFileId: String?) {
if parentFileId == nil {
fileIds.append(fileId)
} else if fileIds.last == parentFileId {
fileIds.append(fileId)
} else if fileIds.last == fileId {
// do noting, because this was caused by navigation bug, onAppear called twice
} else {
var copy = fileIds
copy.removeLast()
while copy.last != parentFileId {
copy.removeLast()
}
copy.append(fileId)
fileIds = copy
}
}
}
And wrap the container a GeometryReader and using the SaveToPageViewModel to follow the recursive view's length:
#State var itemWidth: CGFloat = 0
...
GeometryReader { proxy in
...
RecursiveFolderListView(fileId: "root", selectedItem: $selectedItem, parentFileId: nil, itemWidth: itemWidth)
.environmentObject(viewModel)
...
}
.onReceive(viewModel.$fileIds) { fileIds in
itemWidth = proxy.size.width / CGFloat(fileIds.count)
}
And in the RecursiveFolderListView, change the model data:
RecursiveFolderListView(fileId: item.fileId, selectedItem: $selectedItem, parentFileId: fileId, itemWidth: itemWidth)
.id(theId)
...
}
.onAppear {
model.tryInsert(fileId: fileId, parentFileId: parentFileId)
}

How do I make a SwiftUI Scroll view shrink-to-content?

I have a SwiftUI GUI with two lists stacked on top of each other. Each list has a variable number of items. I'd like both lists to shrink to fit their contents.
If I use VStack around ForEach, the simple case works (see example below). There is a spacer near the bottom, so the lists shift to the top of the window like I expect.
Now, sometimes the lists are large and I want a Scroll view and a maximum height, but I still want the list to shrink when it has fewer items. But as soon as I add the Scroll view, the Scroll view starts to take up all the space it can. If I assign it a maximum, then it doesn't shrink to fit it's contents anymore.
In the example below (as written) I get the resizing behavior I want (without the max size). If I uncomment the Scroll view, then it consumes all the space, if I uncomment the frame modifier, it works, but the size is fixed.
struct ContentView: View {
#State var List1: [String] = [ ]
#State var List2: [String] = [ ]
var body: some View {
VStack {
Button("1-5") {
List1=[ "1" ]
List2=[ "a", "b", "c", "d", "e" ]
}
Button("3-3") {
List1=[ "1", "2", "3" ]
List2=[ "a", "b", "c" ]
}
Button("5-1") {
List1=[ "1", "2", "3", "4", "5" ]
List2=[ "a" ]
}
//ScrollView {
VStack {
ForEach(List1.indices, id: \.self) { idx in
Text(List1[idx])
}
}
//}
//.frame(maxHeight: 40)
Text("middle")
VStack {
ForEach(List2.indices, id: \.self) { idx in
Text(List2[idx])
}
}
Spacer()
Text("last")
}
}
}
You need PreferenceKey to calculate the size of your ScrollView content. Here a getSize function that can help you :
struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
value = nextValue()
}
}
struct SizeModifier: ViewModifier {
private var sizeView: some View {
GeometryReader { geometry in
Color.clear.preference(key: SizePreferenceKey.self, value: geometry.size)
}
}
func body(content: Content) -> some View {
content.overlay(sizeView)
}
}
extension View {
func getSize(perform: #escaping (CGSize) -> ()) -> some View {
self
.modifier(SizeModifier())
.onPreferenceChange(SizePreferenceKey.self) {
perform($0)
}
}
}
You have to compare the height of your content (with getSize) and the height of the ScrollView (with GeometryReader), and set the frame accordingly :
struct SwiftUIView12: View {
#State private var items: [String] = ["One", "Two", "Three"]
#State private var scrollViewSize: CGSize = .zero
var body: some View {
GeometryReader { proxy in
ScrollView {
ForEach(items, id: \.self) { item in
Text(item)
.padding()
}
.frame(maxWidth: .infinity)
.getSize {scrollViewSize = $0}
}
.frame(height: scrollViewSize.height < proxy.size.height ? scrollViewSize.height : .none )
.background(Color.blue.opacity(0.2))
}
.navigationTitle("Test")
.toolbar {
Button("Many items") {
items = (1 ... 30).map { _ in String.random(length: 10) }
}
}
}
}

SWIFTUI - creating a ForEach with Searchbar, text and button

I am building a list which should have more than 100 elements (in the future) but the alignment seems not to work..
Currently i have this...
I think it looks pretty nice atm - but the problem is, when you clicked an element (it doesnt matter which one) every element will be clicked... I tested this by print command.
The searchbar works pretty fine, except from hiding the head - but that is not the problem right now..
When you search for an item, the correct item will be displayed and by clicking on it, the correct command appears...
I am looking for my mistake, but i cant find it.. Maybe you guys can help me :-)
This is my code...
struct RecipeIngredientsView: View {
let myArray = ["Dennis", "Tessa", "Peter", "Anna", "Tessa", "Klaus", "Xyan", "Zuhau", "Clown", "Brot", "Bauer"]
#State private var searchText = ""
#State private var showCancelButton: Bool = false
var body: some View {
VStack
{
HStack
{
HStack
{
Image(systemName: "magnifyingglass")
TextField("Suche", text: $searchText, onEditingChanged: { isEditing in self.showCancelButton = true}, onCommit: {
print("onCommit")
}).foregroundColor(.primary)
Button(action: {
self.searchText = searchText
}){
Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
}
}.padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
.foregroundColor(.secondary)
.background(Color(.secondarySystemBackground))
.cornerRadius(10.0)
if showCancelButton {
Button("Abbrechen")
{
UIApplication.shared.endEditing(_force: true)
self.searchText = ""
self.showCancelButton = false
}
.foregroundColor(Color(.systemBlue))
}
}
.padding(.horizontal)
.navigationBarHidden(showCancelButton)
List {
VStack{
ForEach(myArray.filter{$0.hasPrefix(searchText) || searchText == ""}, id:\.self)
{
searchText in VStack {
CardView(searchText: searchText)
}
}
Spacer()
}
}
.navigationBarTitle(Text("Suche"))
.resignKeyboardOnDragGesture()
}
}
}
struct CardView : View
{
#State var searchText = ""
var body : some View
{
HStack{
Text(searchText)
Spacer()
Button(action: {print("\(searchText) btn pressed!")})
{
Image(systemName: "circle")
.frame(width:25, height:25)
.clipShape(Circle())
}
}
}
}
Thank you guys & merry x-mas!:-)
This happened because of :
List {
VStack{
//.... ForEach loop
}
}
Like that the list contains just one element , which is the VStack
To avoid this your code should be like that :
List {
ForEach(myArray.filter{$0.hasPrefix(searchText) || searchText == ""}, id:\.self)
{ searchText in
CardView(searchText: searchText)
}
}

SwiftUI selection in lists not working on reused cells

Consider the following project with two views. The first view presents the second one:
import SwiftUI
struct ContentView: View {
private let data = 0...1000
#State private var selection: Set<Int> = []
#State private var shouldShowSheet = false
var body: some View {
self.showSheet()
//self.showPush()
}
private func showSheet() -> some View {
Button(action: {
self.shouldShowSheet = true
}, label: {
Text("Selected: \(selection.count) items")
}).sheet(isPresented: self.$shouldShowSheet) {
EditFormView(selection: self.$selection)
}
}
private func showPush() -> some View {
NavigationView {
Button(action: {
self.shouldShowSheet = true
}, label: {
NavigationLink(destination: EditFormView(selection: self.$selection),
isActive: self.$shouldShowSheet,
label: {
Text("Selected: \(selection.count) items")
})
})
}
}
}
struct EditFormView: View {
private let data = 0...1000
#Binding var selection: Set<Int>
#State private var editMode: EditMode = .active
init(selection: Binding<Set<Int>>) {
self._selection = selection
}
var body: some View {
List(selection: self.$selection) {
ForEach(data, id: \.self) { value in
Text("\(value)")
}
}.environment(\.editMode, self.$editMode)
}
}
Steps to reproduce:
Create an app with the above two views
Run the app and present the sheet with the editable list
Select some items at random indexes, for example a handful at index 0-10 and another handful at index 90-100
Close the sheet by swiping down/tapping back button
Open the sheet again
Scroll to indexes 90-100 to view the selection in the reused cells
Expected:
The selected indexes as you had will be in “selected state”
Actual:
The selection you had before is not marked as selected in the UI, even though the binding passed to List contains those indexes.
This occurs both on the “sheet” presentation and the “navigation link” presentation.
If you select an item in the list, the “redraw” causes the original items that were originally not shown as selected to now be shown as selected.
Is there a way around this?
It looks like EditMode bug, worth submitting feedback to Apple. The possible solution is to use custom selection feature.
Here is a demo of approach (modified only part). Tested & worked with Xcode 11.4 / iOS 13.4
struct EditFormView: View {
private let data = 0...1000
#Binding var selection: Set<Int>
init(selection: Binding<Set<Int>>) {
self._selection = selection
}
var body: some View {
List(selection: self.$selection) {
ForEach(data, id: \.self) { value in
self.cell(for: value)
}
}
}
// also below can be separated into standalone view
private func cell(for value: Int) -> some View {
let selected = self.selection.contains(value)
return HStack {
Image(systemName: selected ? "checkmark.circle" : "circle")
.foregroundColor(selected ? Color.blue : nil)
.font(.system(size: 24))
.onTapGesture {
if selected {
self.selection.remove(value)
} else {
self.selection.insert(value)
}
}.padding(.trailing, 8)
Text("\(value)")
}
}
}

SwiftUI How to Create a button that adds an item to a list that contains a navigation link

I've never done a post on this before, so hopefully this is set up correctly.
I'm new to swift, and I want to create a button that adds a new item to a list that contains a navigation link to a file that it would create with linked data from the previous item, and I haven't found any way to do this after spending a few days on research and testing.
This is what my app currently looks like for the layout I want in the end: Q1 , and here is a preview of the different Q1-4 Views I mentioned: Q1-4
I know it's a lot, so let me explain more in depth: I want to have a list contained in what is called 'Q1' (as seen above) that starts out with 'Week 1', and as you click the add button, I want it to add a 'Week 2' and so forth up to 10 weeks. Once you hit 10 weeks, I want the user to have to change to the different view, 'Q2', which then they can add Week 11-20, and so forth until Q4, which limits it to a total of 40 Weeks. I want each week to contain a navigation link to a new view; however, I also want data from the previous week to be carried over as soon as I create the new week, so the user won't have to manually put in the previous week's data.
I know how to do some of this by using a JSON file for the numbers, as I've seen tutorials on that, however, I don't see a point to this, as the only data I need for the Week numbers are 1-40, but I can't seem to get it to work with an array or anything. I do know that I can use an #EnvironmentObject to get the data I need from the other pages, but I'm not exactly sure how to set that up either. Other than that, I'm stuck! Here is my code:
import SwiftUI
struct BillsView: View {
#State private var quarterNumber = 0
let quarterNumbers = [1, 2, 3, 4]
var body: some View {
NavigationView{
VStack {
Section {
Picker("Quarter Number", selection: $quarterNumber) {
ForEach(0 ..< quarterNumbers.count) {
Text("Q\(self.quarterNumbers[$0])")
}
}
.pickerStyle(SegmentedPickerStyle())
.padding(.horizontal)
if quarterNumber == 0 {
Q1View()
} else if quarterNumber == 1 {
Q2View()
} else if quarterNumber == 2 {
Q3View()
} else if quarterNumber == 3 {
Q4View()
}
}
Spacer()
}
.navigationBarTitle("Bills")
.navigationBarItems(leading: EditButton(),
trailing: Button(action: {
//Adds the new week
}){
Image(systemName: "plus.circle.fill")
})
}
}
}
struct Q1View: View {
#State private var weekNumber = 0
let weekNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var body: some View {
List {
NavigationLink(destination: Week1View()) {
Text("Week 1")
}
}
}
}
struct Week1View: View {
var body: some View {
List {
link(label: "Gross Income", destination: GrossIncome())
link(label: "Expenses", destination: Expenses())
}.navigationBarTitle(Text("Week 1"), displayMode: .inline)
}
private func link<Destination: View>(label: String, destination: Destination) -> some View {
return NavigationLink(destination: destination) {
Text(label)
}
}
}
I am not quite sure whether i understood you right, but i made a very simple example which answers the question of your title
import SwiftUI
struct ContentView: View {
#State private var list : [String] = ["Chris"]
#State private var quarterNumber = 0
var body: some View {
Group () {
Button(action: {
self.list.append("whatever")
}) {
Text("tap me")
}
NavigationView{
List(list, id: \.self) { item in
NavigationLink(
destination: View2(text: "hallo")
.navigationBarTitle(Text("Categories"), displayMode: .automatic)
) {
Text("Categories")
}.isDetailLink(false) // damit der Doof nicht rechts das nächste Menu öffnet
}
}
}
}
}
struct View2: View {
var text : String
var body: some View {
Text(text)
}
}