Open onmyway133 opened 3 years ago
I usually define ButtonStyle to encapsulate common styles related to buttons. Here we specify .frame(maxWidth: .infinity) to let this button take the whole width as possible
ButtonStyle
.frame(maxWidth: .infinity)
struct MyActionButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label .font(.headline.bold()) .foregroundColor(.white) .frame(height: 44) .frame(maxWidth: .infinity) .background(Color.green) .cornerRadius(8) } }
If we have 2 buttons both specifying maxWidth: .infinity then they will be divided equally with a spacing specified by our HStack
maxWidth: .infinity
HStack
struct ContentView: View { @State private var showsBottomSheet: Bool = false var body: some View { HStack(spacing: 20) { Button(action: {}) { Text("Left") } .buttonStyle(MyActionButtonStyle()) Button(action: {}) { Text("Right") } .buttonStyle(MyActionButtonStyle()) } .padding(.horizontal, 20) } }
The result looks like below
I usually define
ButtonStyle
to encapsulate common styles related to buttons. Here we specify.frame(maxWidth: .infinity)
to let this button take the whole width as possibleIf we have 2 buttons both specifying
maxWidth: .infinity
then they will be divided equally with a spacing specified by ourHStack
The result looks like below