18

I'm trying to add both Text and Image on Button like:

Button(action: {}) {
        Image("gift")
        Text("Send")
            .padding(.horizontal)
    }
    .padding()
    .foregroundColor(.white)
    .background(Color.gray)
    .cornerRadius(.infinity)

Button looks:

enter image description here

But I want to create this:

enter image description here

Thanks

4 Answers 4

44

Just put the Image and Text inside an HStack

Button(action: {}) {
    HStack {
        Image(systemName: "gift")
        Text("Send")
    }
}
.padding()
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(.infinity)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

As of iOS 14, there's a standard view to accomplish this layout:

Button(action: {}) {
  Label("Send", image: "gift")
    .padding()
    .foregroundColor(.white)
    .background(Color.gray)
    .cornerRadius(.infinity)
}

Also, adding the style modifiers to the content inside the Button view allows the button background to also be tappable. Adding the style modifiers outside the button will result in only the immediate area occupied by the image and text (the body of the Button view) being tappable, but not the surrounding button background (the gray area).

Comments

0

Other solution, using direct constructor and buttonStyle with control size

Button("Send", systemImage: "gift", action: {
    print("send")
})
.buttonStyle(.borderedProminent)
.controlSize(.small)

If you prefere change background color using .accentColor(.gray) and foreground color with foregroundStyle

Button("Send", systemImage: "gift", action: {
    print("send")
})
.buttonStyle(.borderedProminent)
.controlSize(.small)
.accentColor(.gray)
.foregroundStyle(.white)

Comments

-1

SwiftUI

It's working fine for button Text, Image, and Shadow

Button(action: {

            }) {
                HStack {
                    Image(uiImage: UIImage(named: "Login")!)
                        .renderingMode(.original)
                        .font(.title)
                        .foregroundColor(.blue)
                    Text("Login")
                        .font(.title)
                        .foregroundColor(.white)
                }


                .font(.system(size: 15))

                .frame(minWidth: 0, maxWidth: .infinity)
                .background(LinearGradient(gradient: Gradient(colors: [Color("DarkGreen"), Color("LightGreen")]), startPoint: .leading, endPoint: .trailing))
                .cornerRadius(40)
                .frame(width: 200, height: 60, alignment: .center)
                .shadow(color: .red, radius: 5, x: 0, y: 0)

            }

enter image description here

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.