1
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】で標準的な設定画面を作る方法(Toggleだけで簡単実装)

Posted at

SwiftUI では、iOSアプリによくある 設定画面(Setting UI) を
驚くほど簡単に実装できます。
今回のテーマは UIだけで、機能はまだ実装しないシンプルな設定画面です。

🎯 完成イメージ
List 形式の設定項目
右側に ON / OFF のトグルスイッチ
設定名をタップして切り替え可能
iOS純正の設定アプリのようなUIが、わずか数行で作れます。

💻 サンプルコード

import SwiftUI

struct ContentView: View {
    @State var settings: [String : Bool] = [
        "通知" : true,
        "ダークモード" : false,
        "サウンド" : true,
        "広告非表示" : false
    ]
    var body: some View {
        NavigationStack {
            List {
                ForEach(settings.keys.sorted(), id: \.self) { key in
                    Toggle(key, isOn: Binding(
                        get: { settings[key] ?? false },
                        set: { settings[key] = $0 }
                    ))
                }
            }
            .navigationTitle("設定")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

#Preview {
    ContentView()
}

🔍 ポイント解説

仕組み 説明
Dictionary 設定名(String)とON/OFF(Bool)を管理
Toggle 設定項目の右側に表示されるスイッチ
Binding Dictionaryの値を双方向に更新するため
List iOSの標準設定画面のような見た目になる

📦 メリット
SwiftUI標準UIだけで洗練された設定画面が作れる
高度な機能(検索・永続化・並び替え)を後から簡単に追加できる
UIKitで複雑だった設定画面を数行で実装可能

🎉 まとめ
SwiftUIでは、Toggle を使うだけで
標準的な設定画面のUIを簡単に作ることができる。
まずはUIだけ作り、必要になったら機能を追加していくと
無理なく成長できるアプローチになる。

1
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
1
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?