Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Button is not Taking Given Height and Width in SwiftUI

I am new to swiftui. I am making a login screen in which I have to use log in with apple and gmail buttons. I am using a custom view to design the button. Here is the code for that custom button view:

struct CustomSocialButton: View {
    
    var ImgName: String
    
    var body: some View {
        
        
        Button(action: {
                  print("button pressed")

                }) {
                    Image(ImgName)
                    .renderingMode(.original)
                }
        
    }
}

struct CustomSocialButton_Previews: PreviewProvider {
    static var previews: some View {
        CustomSocialButton(ImgName: ImageName.appleSignin.rawValue)
    }
}

I am saving the image names to my constant file.

The problem is when I use it to my actual login view and setting its width and height. It is not taking that height and width. Here is the code for the buttons:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

HStack(spacing: 10) {
                   
          CustomSocialButton(ImgName: ImageName.appleSignin.rawValue)
                .frame(width: 48, height: 48)
                   
          CustomSocialButton(ImgName: ImageName.googleSignin.rawValue)
                .frame(width: 48, height: 48)

       }

This is the screenshot of my login View: login view

Does anyone knows what I am doing wrong?

>Solution :

You need to make image resizable, like

Image(ImgName)
   .renderingMode(.original)
   .resizable()               // << here !!

also might want to add .aspectRatio(contentMode: .fit)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading