I have Button
struct ContentView : View { var body: some View { HStack { Button(action: {}) { Text("MyButton") .color(.white) .font(Font.system(size: 17)) } .frame(height: 56) .background(Color.red, cornerRadius: 0) } } }
But I want to pin it to supreview's edges (trailing to superview's trailing and leading). Like this:
HStack
doesn't help me, and it's expecting. Fixed frame
or width equals UIScree.size
are not flexible solutions.
To make a SwiftUI view take all available width, we use . frame() modifier with maxWidth and maxHeight set to .
SwiftUI makes it easy to create two views that are the same size, regardless of whether you want the same height or the same width, by combining a frame() modifier with fixedSize() – there's no need for a GeometryReader or similar.
By default SwiftUI's views take up only as much space as they need, but if you want that to change you can use a frame() modifier to tell SwiftUI what kind of size range you want to have.
Luckily for us SwiftUI makes this very easy to do. TLDR: To make a view fill the screen or parent view, you can set the maxHeight and maxWidth to . infinity when using the frame view modifier.
You need to use .frame(minWidth: 0, maxWidth: .infinity)
modifier
Add the next code
Button(action: tap) { Text("Button") .frame(minWidth: 0, maxWidth: .infinity) .background(Color.red) } .padding([.leading, .trailing], 20)
Padding modifier will allow you to have some space form the edge.
Keep in mind that the order of modifiers is important. Because modifiers are actually functions that are wrapping the view bellow(they do not change a properties of views)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With