如何使用SwiftUI使按钮和标签共享相同的宽度?

时间:2019-06-26 03:18:12

标签: swift swiftui

struct PasswordGenerator : View {
    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                //123
                Text("Upon clicking the button the password\nwill also be copied so you\ncan simply paste")
                    .lineLimit(nil)
                Spacer()
                    //123
                    Button(action: generatePassword) {
                        HStack {
                            Image("GeneratorGlyph")
                            Text("Generate!").font(.title)
                            }
                            .accentColor(.white)
                                                .frame(minHeight: 56, maxHeight: 112)
                                                .frame(minWidth: 0, maxWidth: .infinity)
                        }
                        .background(LinearGradient(gradient: Gradient(colors: [Color.init(red: 48/255, green: 153/255, blue: 216/255), Color.init(red: 65/255, green: 165/255, blue: 237/255)]), startPoint: .topLeading, endPoint: .bottomTrailing), cornerRadius: 9)
                Spacer()
                }
                .navigationBarTitle(Text("Generator"), displayMode: .inline)
        }
    }
}

所以我有一个按钮和一个Text标签(我在两个标签上都加了一个注释,说123,这样您就更容易看到了),我想知道按钮如何具有与Text标签相同的宽度

1 个答案:

答案 0 :(得分:0)

只需在按钮上添加填充:

.padding(.horizontal, 40)