Home > AI > IOS > SwiftUI >

LibraryContentBuilder

Add @LibraryContentBuilder, you don’t need to use return.

Example:

struct UserProfileView: View {
    var user: User
     
    var body: some View {
        HStack {
            Image(user.imageName)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 60, height: 60)
                .cornerRadius(30)
                .overlay(
                    RoundedRectangle(cornerRadius: 30)
                        .stroke(Color.white, lineWidth: 3)
                )
             
            VStack(alignment: .leading) {
                Text(user.name)
                    .font(.headline)
                Text(user.handle)
                    .font(.subheadline)
                    .foregroundColor(.gray)
            }
        }
    }
}
 
struct User {
    let imageName: String
    let name: String
    let handle: String
}
 
 
 
struct UserProfileLibrary: LibraryContentProvider {
    
    
    @LibraryContentBuilder
    var views: [LibraryItem] {
        let user1 = User(imageName: "john", name: "John Doe", handle: "@johndoe")
        
        LibraryItem(UserProfileView(user: user1), title: "User Profile1", category: .control)
        
        let user2 = User(imageName: "john", name: "John Doe", handle: "@johndoe")
        
        LibraryItem(UserProfileView(user: user2), title: "User Profile2", category: .control)
    }
    
    
    @LibraryContentBuilder
    func modifiers(base: AnyView) -> [LibraryItem] {
        
        LibraryItem(
            base.onAppear {
                print("good")
            },
            title: "User Profile Modifier"
        )
    }
}

Leave a Reply