6

How can I put the image in the Form of swiftUI in order to have the same effect like the picture attached?

enter image description here

I have try with this code, but not working:

struct UserDetails: View {
    @ObservedObject var dm : DataManager
    @Binding var userLoggato : UserModel?
    var body: some View {
        Form{
            HStack{
                Image(systemName: "person")
            }
            Text(userLoggato?.name ?? "")
        }
    }
}

If I use the spacer between the Image I obtain another white line with the image inside. I want my image have the gray background and been positioned like in the attached picture.

Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
  • What do you mean by `same effect`? In circle, paddings, label, background? Would you be more precise? – Asperi Aug 13 '20 at 09:29
  • Can't use the spacer , I want my image stay in the centre top part of the form and have the grey background like the screenshot above. If I use the stack it just add an extra line inside the form. – Damiano Miazzi Aug 13 '20 at 10:17

2 Answers2

3

Here is a demo of possible approach - no spacers - (colors/paddings are configurable, as usual)

Tested with Xcode 11.4

demo

struct UserDetails: View {
    var body: some View {
        Form{
            HStack(alignment: .top) {
                VStack {
                    Image(systemName: "person")
                        .resizable()
                        .frame(width: 60, height: 60)
                        .padding()
                        Text("Person Name").font(Font.title)
                        Text("position title")
                }.padding()

            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            .listRowInsets(EdgeInsets())
            .background(Color(UIColor.lightGray)) // your color here !!

            Text("Email")
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
1

Just add Spacers in HStack:

struct UserDetails: View {
    @ObservedObject var dm : DataManager
    @Binding var userLoggato : UserModel?
    var body: some View {
        Form{
            HStack{
                Spacer()
                Image(systemName: "person")
                Spacer()
            }
            Text(userLoggato?.name ?? "")
        }
    }
}
RealUglyDuck
  • 338
  • 2
  • 13