Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Because the launch of iOS 16, it’s straightforward to create an interactive backside sheet utilizing SwiftUI. All you have to do is to embed a modifier referred to as presentationDetents
in a Sheet view. Earlier, we printed a detailed tutorial to stroll you thru the API. Nonetheless, in case you’ve forgotten its utilization, let’s try the pattern code beneath.
var physique: some View {
VStack {
Button(“Present Backside Sheet”) {
showSheet.toggle()
}
.buttonStyle(.borderedProminent)
.sheet(isPresented: $showSheet) {
Textual content(“That is the expandable backside sheet.”)
.presentationDetents([.medium, .large])
}
Spacer()
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
struct BasicBottomSheet: View { @State personal var showSheet = false var physique: some View { VStack { Button(“Present Backside Sheet”) { showSheet.toggle() } .buttonStyle(.borderedProminent) .sheet(isPresented: $showSheet) { Textual content(“That is the expandable backside sheet.”) .presentationDetents([.medium, .large]) } Spacer() } } } |
You employ the sheet
modifier to create a presentation sheet and insert the presentationDetents
modifer contained in the sheet to manage the scale of the sheet. Each .medium
and .peak
are the built-in detents.
Other than adjusting the peak of the sheet, there aren’t many choices to customise it. However, with the upcoming launch of iOS 16.4, you’ll be capable of create customized backgrounds, have management over the scrolling of the sheet, and lots of extra.
Let’s see what customization choices are launched for the presentation sheet. Please notice that you simply want Xcode 14.3 (or up) to comply with this tutorial.
With the brand new presentationBackground
modifier, now you can change the background colour of the sheet like this:
.presentationBackground(.yellow) |
By making use of a background materials kind, you’ll be able to create a blur impact to the sheet. Right here is the pattern code:
var physique: some View {
ZStack(alignment: .high) {
Picture(“wallpaper”)
.resizable()
.scaledToFill()
.clipped()
.ignoresSafeArea()
Button(“Present Backside Sheet”) {
showSheet.toggle()
}
.tint(.black)
.buttonStyle(.borderedProminent)
.sheet(isPresented: $showSheet) {
Textual content(“That is the expandable backside sheet.”)
.presentationDetents([ .medium, .large])
.presentationBackground(.thinMaterial)
}
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
struct ContentView: View { @State personal var showSheet = false
var physique: some View { ZStack(alignment: .high) { Picture(“wallpaper”) .resizable() .scaledToFill() .clipped() .ignoresSafeArea()
Button(“Present Backside Sheet”) { showSheet.toggle() } .tint(.black) .buttonStyle(.borderedProminent) .sheet(isPresented: $showSheet) { Textual content(“That is the expandable backside sheet.”) .presentationDetents([ .medium, .large]) .presentationBackground(.thinMaterial)
} } } } |
We set the presentation background to .thinMaterial
so as to add a translucent layer between the sheet and the background picture.
Optionally, you’ll be able to create a customized view because the sheet’s background utilizing the modifier like this:
.presentationBackground { Picture(“wallpaper”) } |
This new replace additionally comes with a brand new modifier named presentationCornerRadius
which lets you alter the nook radius of the presentation sheet. Right here is an instance.
In earlier iOS variations, SwiftUI would disable the view behind the underside sheet, which made it unimaginable for customers to work together with it till they dismissed the sheet. Ranging from iOS 16.4, SwiftUI introduces one other modifier referred to as presentationBackgroundInteraction
for builders to allow/disable the interplay.
To allow the background interplay, you connect the presentationBackgroundInteraction
modifier to the sheet view and set its worth to .enabled
:
.presentationBackgroundInteraction(.enabled) |
On this case, customers can work together with the view behind the sheet.
You can even restrict the background interplay to a sure detent. For example, the underside sheet comes with three totally different intents like this:
.presentationDetents([ .height(100), .medium, .large]) |
For those who solely need your customers to work together with the background view for a particular intent, you’ll be able to write the presentationBackgroundInteraction
modifier like beneath:
.presentationBackgroundInteraction(.enabled(upThrough: .peak(100))) |
By default, when a scroll view is included within the backside sheet, the sheet usually expands to the subsequent detent as a person swipes up on the scroll view. For those who don’t know what this implies, let’s try one other instance.
For instance, you current an inventory of things in a backside sheet like this:
.sheet(isPresented: $showSheet) { Record(1...20, id: .self) { index in Textual content(“Merchandise (index)“) } .presentationDetents([.medium, .large]) } |
Once you activate the underside sheet, it is going to be introduced as much as the medium detent. Nonetheless, you couldn’t scroll by way of the listing. Once you swipe up on the scroll view, the sheet merely expands to full display.
In an effort to let customers scroll by way of the listing even the sheet is half-opened, you’ll be able to embed the presentationContentInteraction
modifier and set its worth to .scrolls
:
.sheet(isPresented: $showSheet) { Record(1...20, id: .self) { index in Textual content(“Merchandise (index)“) } .presentationDetents([.medium, .large]) .presentationContentInteraction(.scrolls) } |
Now when the sheet is half-opened, customers can nonetheless swipe up/right down to scroll by way of the listing.
To resize the sheet, customers can maintain and drag the drag indicator.
On the time of this writing, Apple remains to be testing iOS 16.4 in beta, however it’s anticipated to be launched to the general public quickly. If you want to experiment with the brand new SwiftUI modifications, make certain to obtain Xcode 14.3 (beta).