SwiftUI_visionOS_Note

How to use List()

Jan 4, 2024
3 min read|
  • VStack with a list
  • List makes modifying and editing super easy.

1. Create a list with list of text

  • Simply use List() and Text() to create a base code
List {
            Text("Hello, World")
            Text("Hello, World")
            Text("Hello, World")

        }
swift

2. Use array and ForEach()

  • First, we create array of string named fruits including list of fruits.
@State var fruits: [String] = [
        "apple", "orange", "banana", "kiwi"
    ]
swift
  • Import fruits into ForEach.
    • Declare the id with \.self
      • fruit in will write each array of string value
List{
	ForEach(fruits, id: \.self) { fruit in
			Text(fruit)
	}
}
swift
  • .capitalized after the text will make it capitaliezd
Text(fruit.capitalized)
swift

3. Add a Section of the list

  • Section() will create header: and content:
    • We write a header text on the header
      • header: {Text("Fruits)}
    • and we add a content in the curly brackets
Section {ForEach(fruits, id: \.self) {fruit in
	Text(fruit)
	}
} header: {
	Text("Fruitss")
	}
swift

4. Delete an Item (Swipe)

  • add .onDelete(perform: delete) after Section{content}
Section{...} .onDelete(perform: delete)
swift

  • onDelete will perform delete from function we created.
struct ContentView: View {
	var body: some View {...}
 	func delete(indexSet: IndexSet){
		fruits.remove(atOffsets: indexSet)
	} 
}
swift

  • Now we can swipe to delete

5. Delete an Item (Edit)

  • By adding EditButton() on the toolbar will perform the edit
  • by clicking edit, delete button will show on the list
List{...}
 	.toolbar{
		ToolbarItem(placement: .bottomOrnament){
			EditButton()
		}
	}
swift

#toolbar

6. Move an Item (Edit)

  • Add .onMove after Section{}
Section{...}
	.onDelete(...)
 	.onMove(perform: { indices, newOffset in
		fruits.move(
			fromOffsets: indices,
			toOffset: newOffset
	}
swift

  • Extract the function to make it simpler
func move(indices: IndexSet, newOffset: Int){
			fruits.move(
			fromOffsets: indices,
			toOffset: newOffset
	)
}
swift

  • Rewrite .onMove
.onMove(perform: move)
swift

7. Add an Item .append()

  • Add .topBarTrailing as a new toolbar button
ToolbarItem(placement: .topBarTrailing){...}
swift
  • Add a button with .append string value.
    • Pressing a button will add “peach” in the fruits array
Button("Button"){
	fruits.append("peach")
}
swift

  • Extract button function
func add() {
	fruits.append("peach")
}
swift
Button("Button"){
	add()
}
swift

  • Extract the button as a separate View
var addButton: some View {
	Button("Button"){
		add()	
	}
}
swift

  • We can simplify the toolBarItem
ToolbarItem(placement: .topBarTrailing){addButton}
swift

8. Add a list style

  • We can change the style of list by adding .listStyle()
    • Most of styles aren’t supported in VisionOS
.listStyle(...)
swift

Apple developer (List of Style)

9. List Style

List {
                Section {
                    ForEach(fruits, id: \.self) {fruit in
                        Text(fruit)
                    }
                    .onDelete(perform: delete)
                    .onMove(perform: move)

                } header: {
                    HStack{
                        Image(systemName: "flame.fill")
                         Text("Fruits").font(.largeTitle)
                    }.padding().glassBackgroundEffect().frame(depth: 100) 
                }

                
                Section {
                    ForEach(veggies, id: \.self) {veggy in
                            Text(veggy)
                             .frame(maxWidth: .infinity, maxHeight: .infinity)
                            .background(.blue.opacity(0.9))
                            .padding(.vertical) 
                    }
                    .onDelete(perform: delete)
                    .onMove(perform: move)
                     .listRowBackground(Color.green)
 
                } header: {
                    Text("Veggies")
                }
            }
swift

  • Creating a list with an array of text, adding sections, deleting items, moving items, and adding new items.
  • Changing the list style
  • Extracting functions and views

#visionOS #applevisionpro #swiftUI #appdevelopment

This video provides the opportunity to experience a spatial video on my Quest 2. The immersion was remarkable, similar to viewing a 3D movie up close with 3D glasses. However, I noticed that the performance fell short when it came to distant fixtures, scenes, or panoramic views. #VR #SpatialVideo #Quest2

\\

This video provides the opportunity to experience spatial video on my Quest 2. The immersion was remarkable, similar to viewing a 3D movie up close with 3D glasses. However, the performance was lacking when it came to distant fixtures, scenes, or panoramic views.

Subscribe to our newsletter

Get the latest news and updates from our team