Warning: Undefined variable $post in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/functions.php on line 12

Warning: Attempt to read property "ID" on null in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/functions.php on line 12

Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33

【SwiftUI】Toggleの使い方まとめ!ON / OFFのスイッチを作ってみよう

記事内に商品プロモーションを含む場合があります


Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33

Toggleビューは従来のUISwitchに該当する機能で、ON/OFFの切替をするときに使用します。

このページではこのToggleビューの使い方について解説します。

基本の書き方

struct ContentView: View {
    @State var checked = true // ① スイッチのON / OFF を管理する変数を用意
    var body: some View {
        Toggle(isOn: $checked){  // ② ON / OFFを管理
            Text(checked ? "ON" : "OFF") // ③ ラベルの表示
        }
    }
}

ON / OFFスイッチの表示するには、Toggle(isOn: $checked)のように書きます。

引数のisOnでスイッチの状態を管理し、値がTrueならON、FalseならOFFということになります。

isOnの値はスイッチをタップするたびに切り替わるので、@State等を使って変数を宣言して渡してあげればいいです。

関連記事

【SwiftUI】Textビューの使い方まとめ【SwiftUI】Imageビューの使い方まとめ【SwiftUI】TextFieldビューの使い方まとめ【SwiftUI】Buttonビューの使い方まとめ

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です


Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

Warning: Trying to access array offset on false in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/plugins/siteguard/really-simple-captcha/siteguard-really-simple-captcha.php on line 352

CAPTCHA



Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21

Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27

Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30

Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33