[Xcode/SwiftUI] ContentUnavailableViewなるものが便利そう

実装

iOS17から実装可能らしい

import SwiftUI

enum ContentUnavailableType: String, CaseIterable, Identifiable {
    case simple
    case withText
    case withSpecificText
    case withButton

    var id: String { self.rawValue }
}

struct HomeView: View {
    @State private var selectedContentUnavailableType: ContentUnavailableType = .simple

    var body: some View {
        VStack {
            Picker("Select Type", selection: $selectedContentUnavailableType) {
                Text("シンプル").tag(ContentUnavailableType.simple)
                Text("テキスト").tag(ContentUnavailableType.withText)
                Text("特定テキスト").tag(ContentUnavailableType.withSpecificText)
                Text("ボタン").tag(ContentUnavailableType.withButton)
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding()
            Spacer()
            switch selectedContentUnavailableType {
            case .simple:
                ContentUnavailableView.search
            case .withText:
                ContentUnavailableView(
                    "No Connection",
                    systemImage: "wifi.slash",
                    description: Text("Please check your internet connection")
                )
            case .withSpecificText:
                ContentUnavailableView.search(text: "Titanic")
            case .withButton:
                ContentUnavailableView {
                    Image(systemName: "wifi.slash")
                } description: {
                    Text("Please check your internet connection")
                } actions: {
                    Button("Try Again") {
                        Logger.info("Try Again")
                    }
                }
            }
            Spacer()
        }
        .navigationTitle("Content Unavailable Types")
        .padding()
    }
}