SwiftUI: how to size to fit a Button to expand to fill a VStack or HStack parent View?
Setting .infinity
as the maxWidth
, the frame(minWidth: maxWidth: minHeight:)
API can be used to make a subview expand to fill:
VStack(alignment: .center, spacing: 20) {
NavigationLink(destination: CustomView()) {
Text("Button")
}.frame(minWidth: 100, maxWidth: .infinity, minHeight: 44)
.background(Color.primary)
Button(action: { self.isShowingAlert = true }) {
Text("Another Button")
}.frame(minWidth: 100, maxWidth: .infinity, minHeight: 44)
.background(Color.primary)
}.frame(width: 340)
.background(Color.secondary)