0Pricing
SwiftUI Academy · درس

إضافة حشوة حول View

استخدم الحشوة لإنشاء مساحة مريحة حول العناصر

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

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Views Feel Cramped

By default a SwiftUI view sits flush against its neighbors and the screen edge. Padding is the breathing room you add so content never feels squeezed.

Meet the padding Modifier

The padding modifier adds space on the outside of a view. Attach it like any other modifier and SwiftUI grows the view's footprint to make room.

Text("Hello")
    .padding()

The Default Amount

Calling padding() with no value uses a system default that adapts to the platform, usually around 16 points. It is a safe, consistent starting point.

Setting an Exact Amount

Pass a number to control the gap precisely. Here padding(40) adds 40 points of space on every side of the text.

Text("Spaced")
    .padding(40)

Padding One Edge

You can target a single edge instead of all four. This adds 30 points only on top, leaving the other sides untouched.

Text("Top gap")
    .padding(.top, 30)

Horizontal and Vertical

The .horizontal and .vertical edge sets are handy shortcuts. This pads the left and right sides at once with one clean call.

Text("Sides")
    .padding(.horizontal, 24)

Combining Edge Sets

Stack two padding calls to mix amounts per direction. Here the view gets wide horizontal margins and a smaller vertical gap.

Text("Mixed")
    .padding(.horizontal, 32)
    .padding(.vertical, 8)

Order Matters

Modifiers apply outward in order, so where you place padding changes the result. Padding before a background pushes the color outward with the content.

Text("Hi")
    .padding()
    .background(.yellow)

Padding Then Background

Swap the order and the difference is clear: a background applied first, then padding, leaves the color tight to the text with empty space around it.

Text("Hi")
    .background(.yellow)
    .padding()

Padding on Containers

Padding works on stacks and any view, not just text. Wrap a VStack and pad it to push the whole group inward from the screen edges.

VStack {
    Text("Line one")
    Text("Line two")
}
.padding()

Padding vs Margins

SwiftUI has no separate margin concept. Padding is your single tool for spacing, and chaining it gives you full control over every edge.

Quick Check

Test your grip on the padding modifier.

Recap

Nice work! You learned that padding adds breathing room outside a view, with defaults, exact amounts, specific edges, and an order that shapes backgrounds.

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

هل درس «إضافة حشوة حول View» مجاني؟

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

ماذا ستتعلم في «إضافة حشوة حول View»؟

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

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

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

كم من الوقت يستغرق درس «إضافة حشوة حول View»؟

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

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

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

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

  1. إضافة حشوة حول View
  2. تحديد أحجام View باستخدام frame
  3. الخلفيات ونصف قطر الزوايا
  4. Spacer وأولوية التخطيط
← العودة إلى SwiftUI Academy