Updated for Xcode 12.0
What happens if you create a dynamic list and put more than one thing in each row? SwiftUI’s solution is simple, flexible, and gives us great behavior by default: it creates an implicit HStack
to hold your items, so they automatically get laid out horizontally.
For example, if we wanted to make a row where we had a small picture on the left and the remaining space be allocated to a text field, we’d start with a struct to hold our data like this:
struct User: Identifiable {
var id = UUID()
var username = "Anonymous"
}
I’ve given both of those default values to make our example easier.
Once we have that, we could create an array of three users, and show them in a dynamic list, like this:
struct ContentView: View {
let users = [User(), User(), User()]
var body: some View {
List(users) { user in
Image("paul-hudson")
.resizable()
.frame(width: 40, height: 40)
Text(user.username)
}
}
}
SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.