LoginSignup
2
2

【SwiftUI】sheetの背景を透過する方法

Posted at

はじめに

iOS16.4でついに標準モディファイアsheetの背景透過が実現できるようになりました!

基本実装

import SwiftUI

struct ContentView: View {
    @State private var isPresented = false
    var body: some View {
        Button {
            isPresented = true
        } label: {
            Text("表示")
        }
        .edgesIgnoringSafeArea(.all)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(LinearGradient(
            gradient: Gradient(colors: [.purple, .pink, .orange, .yellow]),
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        ))
        .sheet(isPresented: $isPresented) {
            Button {
                isPresented = false
            } label: {
                Text("閉じる")
            }
            .padding(30)
            .background(Color.green)
            .cornerRadius(10)
+           .presentationBackground(Color.clear)
        }
    }
}

simulator_screenshot_7C3B39FE-8B16-40D2-8322-DF6417CDFAF9.png

ガラスのような透過

import SwiftUI

struct ContentView: View {
    @State private var isPresented = false
    var body: some View {
        Button {
            isPresented = true
        } label: {
            Text("表示")
        }
        .edgesIgnoringSafeArea(.all)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(LinearGradient(
            gradient: Gradient(colors: [.purple, .pink, .orange, .yellow]),
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        ))
        .sheet(isPresented: $isPresented) {
            Button {
                isPresented = false
            } label: {
                Text("閉じる")
            }
            .padding(30)
            .background(Color.green)
            .cornerRadius(10)
+           .presentationBackground(Material.ultraThinMaterial)
        }
    }
}

Simulator Screenshot - iPhone 14 Pro - 2023-05-24 at 18.04.10.png

おわり

最近流行りのスケスケUIはMaterialを使うといい感じにできます!!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2