1

I'm noob in swiftUI. I have created a subview which I'm using. In the VStack Cards(next to the car) the inside Text are not aligned with equal spacing from the left(like 150km and 38ºc are not aligned). I want the text within the card to start after a spacing of 10 from the left of card. I have added .padding(.left ,10) to card but its not working. I'm attaching the code with it. Please correct me if I'm using the wrong approach.

struct CarInfoCard: View {
    @State var cardHeading = ""
    @State var cardUnit = ""
    @State var cardType = ""
    @State var cardNotify = false
    
    var body: some View {
        
        VStack{
           
            HStack {
                
                
                VStack(alignment: .leading){
                    
                    HStack(alignment: .lastTextBaseline, spacing: 2) {
                        Text(cardHeading)
                            .font(.system(size: 40, weight: .bold, design: .rounded))
                            .foregroundColor(Color("ColorButtonLogo"))
                        
                        Text(cardUnit)
                            .font(.system(size: 25, weight: .bold, design: .rounded))
                            .foregroundColor(Color("ColorButtonLogo"))
                    }
                    
                    
                    Text(cardType)
                        .font(.system(size: 20, design: .rounded))
                        .foregroundColor(Color("ColorButtonLogo"))
                    
                }
            }
            
        }
        .frame(width: 150, height: 90)
        .padding(.top,10)
        .padding(.bottom, 5)
//added padding to the left so that text always starts after spacing of 10. This does not work
        .padding(.leading, 10)
                
        .background(Color("ColorButton"))
        .cornerRadius(20)
    }
}

enter image description here

1 Answer 1

1

Just remove all those redundant stacks and add alignment to frame, like (with substituted colors)

demo

 VStack(alignment: .leading) {    // << here one !!

      HStack(alignment: .lastTextBaseline, spacing: 2) {
            Text(cardHeading)
                 .font(.system(size: 40, weight: .bold, design: .rounded))
                 .foregroundColor(Color.blue)

            Text(cardUnit)
                 .font(.system(size: 25, weight: .bold, design: .rounded))
                 .foregroundColor(Color.blue)
      }


      Text(cardType)
            .font(.system(size: 20, design: .rounded))
            .foregroundColor(Color.blue)

 }
.frame(width: 150, height: 90, alignment: .leading)   // << here !!
.padding(.top,10)
.padding(.bottom, 5)
.padding(.leading, 10)      // now customise as needed !!
.background(Color.yellow)
.cornerRadius(20)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.