Saturday, 28 September 2019

Table Cell Designing in SwiftUI

Table Cell Designing in SwiftUI

Code

ContactRow.swift
This code creates a single reusable view with shadow and radius.


import SwiftUI

struct ContactView: View {
    var body: some View {
        HStack(alignment: .center, spacing: 20) {
            Image("inder")
                .resizable()
                .background(Color.white)
                .clipped()
                .clipShape(Circle())
                .frame(width: 100, height: 100, alignment: .leading)
                .padding(5)
            
            VStack(alignment: .leading, spacing: 2) {
                Text("Contact Name")
                    .font(.title)
                
                Text("9001000663")
                    .font(.subheadline)
            }
            .frame(maxWidth: .infinity,alignment: .leading)
        }
        .background(Color.white)
        .cornerRadius(8)
        .shadow(radius: 5, x: 0, y: 4)
    }
}

struct ContactView_Previews: PreviewProvider {
    static var previews: some View {
        ContactView()
    }

}

ContentView.swift
Add following code in add ContentView.swift file 
Note:- ContactDetailView() is an another view you can remove it if you don't want to create linking for now



import SwiftUI

struct ContactListView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(0..<10){ _ in
                    NavigationLink(destination: ContactDetailView()) {
                        ContactView()
                            .padding(.trailing, -25)
                            .padding(.leading, -10)
                    }
                }
            }
            .navigationBarTitle(NavigationTitle.contacts)
        }
    }
}

struct ContactListView_Previews: PreviewProvider {
    static var previews: some View {
        ContactListView()
    }
}

No comments:

Post a Comment