0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】dismissを使って手軽に遷移元画面に戻る

Posted at

構文

  • 環境変数dismissを使用することでお手軽に前の画面に戻ることができる。
@Environment(\.dismiss) private var dismiss

使用例

  • NavigationStack等で遷移してきた場合にdismiss()を使用することで、元の画面に戻ることができる。

このサンプルコードには生成AIを使用しています。

import SwiftUI
struct DetailView: View {

    @Environment(\.dismiss) private var dismiss

    var body: some View {
        VStack {
            Text("詳細画面です")
                .font(.largeTitle)
                .padding()

            Button("前の画面に戻る (Dismiss)") {
                dismiss()
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .navigationTitle("詳細")
    }
}
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?