article

Monday, February 21, 2022

SwiftUI View Profile Page - Sheets and ForEach

SwiftUI View Profile Page - Sheets and ForEach

ContentView.swift
 
//
//  ContentView.swift
//  SwiftUIProject
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    
    @State var data: userlist?
    
    var body: some View {
        NavigationView {
            
            VStack {
                HStack(spacing: 16) {
                    Image("pic")
                        .resizable()
                        .scaledToFill()
                        .frame(width: 50, height: 50)
                        .clipped()
                        .cornerRadius(50)
                        .overlay(RoundedRectangle(cornerRadius: 44)
                                    .stroke(Color(.label), lineWidth: 1)
                        )
                        .shadow(radius: 5)
                    
                    
                    VStack(alignment: .leading, spacing: 4) {
                        Text("cairocoders")
                            .font(.system(size: 24, weight: .bold))
                        
                        HStack {
                            Circle()
                                .foregroundColor(.green)
                                .frame(width: 14, height: 14)
                            Text("online")
                                .font(.system(size: 12))
                                .foregroundColor(Color(.lightGray))
                        }
                    }
                    
                    Spacer()
                    Button {
                        
                    } label: {
                        Image(systemName: "gear")
                            .font(.system(size: 24, weight: .bold))
                            .foregroundColor(Color(.label))
                    }
                }//End HStack
                .padding()
                
                ScrollView {
                    ForEach(user){i in
                        Button(action: {
                            data = .init(id: i.id, name: i.name, image: i.image, pic: i.pic)
                        }) {
                            VStack {
                                HStack(spacing: 16) {
                                    Image(i.image)
                                        .resizable()
                                        .scaledToFill()
                                        .frame(width: 50, height: 50)
                                        .clipped()
                                        .cornerRadius(50)
                                    
                                    VStack(alignment: .leading) {
                                        Text(i.name)
                                            .font(.system(size: 16, weight: .bold))
                                        Text("View Profile")
                                            .font(.system(size: 14))
                                            .foregroundColor(Color(.lightGray))
                                    }
                                    Spacer()
                                    
                                    Text("22m")
                                        .font(.system(size: 14, weight: .semibold))
                                }
                                Divider()
                                .padding(.vertical, 8)
                                
                            }.padding(.horizontal)
                        }
                    }//End ForEach
                    .padding(.bottom, 50)
                    .sheet(item: $data) { rs in
                        Profile(viewdata: rs)
                    }
                }// End scrollview
            }
            .navigationBarHidden(true)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct userlist : Identifiable {
     
    var id : Int
    var name : String
    var image : String
    var pic : String
}

var user = [
    userlist(id: 0, name: "Taylor", image: "photo1", pic: "1"),
    userlist(id: 1, name: "Mari", image: "photo2", pic: "2"),
    userlist(id: 2, name: "Sandra", image: "photo3", pic: "3")
]
Profile.swift
 
//
//  Profile.swift
//  SwiftUIProject
//
//  Created by Cairocoders
//

import SwiftUI

struct Profile : View {
    
    @Environment(\.presentationMode) var presentationMode
    
    let viewdata : userlist
    
    var body : some View{
        
        ZStack{
            Image(viewdata.pic).resizable().edgesIgnoringSafeArea(.all)
            
            VStack{
                HStack{
                    
                    Button(action: {
                    }) {
                        Image(systemName: "slider.horizontal.3").foregroundColor(Color.black).font(.title)
                    }
                    
                    Spacer()
                    
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Image(systemName: "xmark").foregroundColor(Color.black).font(.title)
                    }
                }
                
                Spacer()
                
                ZStack(alignment: .top) {
                    VStack{
                        HStack{
                            VStack(alignment: .leading, spacing: 10) {
                                
                                Text("\(viewdata.name),").font(.title)
                                Text("19")
                            }
                            
                            Spacer()
                            
                            HStack(spacing: 8){
                                Image(systemName: "mappin.and.ellipse")
                                
                                Text("2 Miles")
                                
                            }.padding(8)
                            .background(Color.black.opacity(0.1))
                            .cornerRadius(10)
                            
                        }.padding(.top,35)
                        
                        Text("Hi! My name \(viewdata.name). I like sharing my thoughts and adore people who except me the way I am. Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit. Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.").padding(.top)
                        
                    }.padding()
                    .background(Blurview())
                    .clipShape(BottomShape())
                    .cornerRadius(25)
                    
                    ZStack{
                        Button(action: {
                        }) {
                            Image(systemName: "message.and.waveform")
                                .frame(width: 20, height: 20)
                                .padding(20)
                                .background(Color.white)
                                .clipShape(Circle())
                                .font(.title)
                                .foregroundColor(Color.green)
                        }
                        
                        Circle().stroke(Color.green, lineWidth: 5).frame(width: 70, height: 70)
                        
                    }.offset(y: -35)
                    
                    HStack{
                        Button(action: {
                            
                        }) {
                            Image(systemName: "suit.heart")
                                .frame(width: 25, height: 20)
                                .padding()
                                .background(Color.white)
                                .clipShape(Circle())
                                .font(.title)
                                .foregroundColor(Color.red)
                        }
                        
                        Spacer()
                        
                        Button(action: {
                            
                        }) {
                            Image(systemName: "hand.thumbsup")
                                .frame(width: 25, height: 25)
                                .padding()
                                .background(Color.white)
                                .clipShape(Circle())
                                .font(.title)
                                .foregroundColor(Color.blue)
                        }
                        
                    }.offset(y: -25)
                    .padding(.horizontal,35)
                        
                }
            }.padding()
        }
    }
}

struct BottomShape : Shape {
    
    func path(in rect: CGRect) -> Path {
        
        return Path{path in

            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: rect.width, y: 0))
            path.addArc(center: CGPoint(x: rect.width / 2, y: 0), radius: 42, startAngle: .zero, endAngle: .init(degrees: 180), clockwise: false)
            
        }
    }
}

struct Blurview : UIViewRepresentable {
    
    func makeUIView(context: UIViewRepresentableContext<Blurview>) -> UIVisualEffectView {
        
        let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialLight))
        
        return view
    }
    
    func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Blurview>) {
        
    }
}

struct Profile_Previews: PreviewProvider {
    static var previews: some View {
        Profile(viewdata: userlist(id: 1, name: "Taylor", image: "photo1", pic: "1"))
    }
}

Related Post