まえがき
2021年のWWDCでUIKitでハーフモーダルが発表され、
SwiftUIでも欲しい、、、!
自作したり、
PartialSheet https://github.com/AndreaMiotto/PartialSheet
を使っていたりしましたが、
公式にiOS16から出たので、どんなものか軽く調査していきます。
struct ContentView: View {
@State var showModalSheet = false
var body: some View {
Button("シートを開く") {
showModalSheet = true
}
.sheet(isPresented: $showModalSheet) {
ZStack {
Color.gray.opacity(0.3)
Text("test")
}
.presentationDetents([.height(100), .fraction(1)])
}
}
}
.sheetの中のViewに
func presentationDetents(_ detents: Set<PresentationDetent>) -> some View
をつけるだけのようです。
引数のデテントdetentは戻り止めという意味で、留め具的な意味のようです。
static let large: PresentationDetent
高さが画面一杯のデテント。
static let medium: PresentationDetent
画面の半分の高さのシートのシステムディテントです。
カスタム デテントを作成する
static func custom<D>(D.Type) -> PresentationDetent
高さを計算したカスタム デテントを作成します
static func fraction(CGFloat) -> PresentationDetent.
指定された分数の高さを持つ、カスタム デテン です。
0.5と指定すれば、画面の半分
0.1と指定すれば、画面の1/10 表示されます
static func height(CGFloat) -> PresentationDetent.
指定された高さのカスタム デテントです。
あとがき
一回使ってみようかとおもいます、
デザイン周りの設定がないんでしょうか。
ちょっと探しましたが見つかりませんでした
PartialSheet https://github.com/AndreaMiotto/PartialSheet
のように細かい設定ができれば良いんですが。
Reference
presentationdetents(_:)
https://developer.apple.com/documentation/swiftui/asyncimage/presentationdetents(_:)
PresentationDetent
https://developer.apple.com/documentation/swiftui/presentationdetent