State による表示の切り替え
ブール値の状態を使ってビューを表示または非表示にします。
「State による表示の切り替え」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Show and Hide on Demand
A boolean @State can decide whether a view is visible. Flip it and SwiftUI shows or hides content for you. 👀
@State private var isVisible = falseConditional Views
Inside body you can use a plain if statement. When the condition is true, SwiftUI includes the view; when false, it leaves it out.
if isVisible {
Text("Now you see me")
}A Button to Flip It
Toggle the boolean from a button action. The tiny toggle() method flips true to false and back.
Button("Toggle") {
isVisible.toggle()
}Wire It Together
Combine the button and the conditional Text in a VStack. Tapping reveals or hides the message as the state changes.
VStack {
Button("Toggle") { isVisible.toggle() }
if isVisible { Text("Hello") }
}The Full View
Here is the complete view: one boolean, one button, and one conditionally shown Text.
struct PeekView: View {
@State private var isVisible = false
var body: some View {
VStack {
Button("Toggle") { isVisible.toggle() }
if isVisible { Text("Hello") }
}
}
}Toggle the Label Text
You can also drive a label from the same boolean, so the button reads Show or Hide depending on the current state.
Button(isVisible ? "Hide" : "Show") {
isVisible.toggle()
}Hidden vs Removed
An if statement truly removes the view from the layout. Other views shift to fill the space, unlike just making something transparent.
Keep Space with opacity
If you want a view to disappear but keep its spot, change its opacity instead of removing it with an if.
Text("Hi").opacity(isVisible ? 1 : 0)Pair It with a Toggle Control
A SwiftUI Toggle control binds directly to the boolean, giving users a switch that flips your state without a custom button.
Toggle("Show details", isOn: $isVisible)One Boolean, Many Reactions
The same state value can control several views at once. Change it in one place and every dependent view reacts together.
A Smoother Reveal
Wrap the change in withAnimation and the show or hide will fade in nicely. State plus a touch of animation feels polished.
withAnimation { isVisible.toggle() }Quick Check
How do you make a Text appear only when a boolean state is true?
Recap
A boolean @State plus an if (or opacity) lets you show and hide views. Flip it with toggle and SwiftUI redraws to match. 🔀
よくある質問
「State による表示の切り替え」レッスンは無料ですか?
はい。「State による表示の切り替え」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「State による表示の切り替え」で何を学びますか?
ブール値の状態を使ってビューを表示または非表示にします。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「State による表示の切り替え」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- View に State が必要な理由
- @State プロパティの宣言
- タップ カウンターの作成
- State による表示の切り替え