< How to let users import videos using PhotosPicker | How to control spacing around individual views using padding > |
Updated for Xcode 14.2
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.
For example, you could create a button with a 200x200 tappable area like this:
Button {
print("Button tapped")
} label: {
Text("Welcome")
.frame(minWidth: 0, maxWidth: 200, minHeight: 0, maxHeight: 200)
.font(.largeTitle)
}
Download this as an Xcode project
Or you could make a text view fill the whole screen (minus the safe area) by specifying a frame with zero for its minimum width and height, and infinity for its maximum width and height, like this:
Text("Please log in")
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.font(.largeTitle)
.foregroundColor(.white)
.background(.red)
Download this as an Xcode project
Note: if you want a view to go under the safe area, make sure you add the ignoresSafeArea()
modifier.
SPONSORED Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.