Understanding Padding in SwiftUI: A Simple Guide

thevenkatthevenkat
1 min read

It's adding extra space around the object. It can adjust dynamically based on your text content.

Basic Example with Padding:

Text("Hello World..!")
    .padding() // Adds default padding on all sides
    .background(Color.cyan)
  • This will add default padding around the text and give it a cyan background.

Custom Padding on All Sides:

Text("Custom Padding")
    .padding(20) // Adds 20 points of padding on all sides
    .background(Color.green)
  • Adds 20 points of padding on all sides, with a green background to show the effect.

Padding Only on Specific Sides:

Text("Horizontal Padding")
    .padding(.horizontal, 30) // Adds 30 points of padding on left and right
    .background(Color.blue)
  • Adds 30 points of padding on the left and right, leaving the top and bottom unchanged.

Combining Different Padding Values:

Text("Different Padding")
    .padding(.top, 10) // Adds padding only at the top
    .padding(.bottom, 20) // Adds padding only at the bottom
    .padding(.horizontal, 40) // Adds padding to the left and right
    .background(Color.red)
  • You can specify different padding values for each side (top, bottom, left, and right).
0
Subscribe to my newsletter

Read articles from thevenkat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

thevenkat
thevenkat

I am a Senior iOS Developer, crafting Native iOS applications using Swift. My passion lies in creating innovative solutions that enhance user experience and drive customer satisfaction. I excel in problem-solving, code optimization, and staying updated with the latest trends and technologies in the iOS ecosystem. I thrive in collaborative environments and am always eager to share knowledge and learn from others.