0Pricing
SwiftUI Academy · درس

تبديل الظهور باستخدام الحالة

أظهر View أو أخفها باستخدام حالة منطقية

تبديل الظهور باستخدام الحالة درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 = false

Conditional 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. 🔀

الأسئلة الشائعة

هل درس «تبديل الظهور باستخدام الحالة» مجاني؟

نعم — نص درس «تبديل الظهور باستخدام الحالة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «تبديل الظهور باستخدام الحالة»؟

أظهر View أو أخفها باستخدام حالة منطقية تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تبديل الظهور باستخدام الحالة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا تحتاج View إلى الحالة
  2. إعلان خصائص @State
  3. إنشاء عدّاد للنقرات
  4. تبديل الظهور باستخدام الحالة
← العودة إلى SwiftUI Academy