在SwiftUI中将文本和图像添加到按钮

时间:2019-09-22 11:37:46

标签: ios swiftui

我正在尝试在Text上同时添加ImageButton,例如:

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

按钮外观:

enter image description here

但是我要创建这个:

enter image description here

谢谢

2 个答案:

答案 0 :(得分:2)

只需将ImageText放在HStack内…

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

enter image description here

答案 1 :(得分:0)

SwiftUI

按钮TextImageShadow的运行正常

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