iOS
UIView
padding
Swift
iOSDevelopment

Adding padding to an UIView

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

UIView does not have a built-in padding property the way web layout systems do. In UIKit, "padding" is usually expressed through Auto Layout constraints, layoutMargins, or a custom subclass that applies insets to its content.

The right choice depends on where the padding belongs. If the padding is only part of one screen layout, constraints are usually enough. If the padding is part of a reusable component contract, margins or a custom subclass are often cleaner.

Use Auto Layout Constraints for Screen-Level Padding

For one-off screen layout, the most direct answer is to pin the content subview to its container with inset constants:

swift
1import UIKit
2
3final class PaddedCardViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let container = UIView()
8        container.translatesAutoresizingMaskIntoConstraints = false
9        container.backgroundColor = .secondarySystemBackground
10
11        let label = UILabel()
12        label.translatesAutoresizingMaskIntoConstraints = false
13        label.numberOfLines = 0
14        label.text = "Padding in UIKit usually comes from constraints."
15
16        view.addSubview(container)
17        container.addSubview(label)
18
19        NSLayoutConstraint.activate([
20            container.centerXAnchor.constraint(equalTo: view.centerXAnchor),
21            container.centerYAnchor.constraint(equalTo: view.centerYAnchor),
22            container.widthAnchor.constraint(equalToConstant: 260),
23
24            label.topAnchor.constraint(equalTo: container.topAnchor, constant: 16),
25            label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 16),
26            label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -16),
27            label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -16)
28        ])
29    }
30}

This is usually the clearest option because the insets are visible right where the layout is defined.

Use layoutMargins for Reusable Container Insets

If a container view should define a reusable content area, layoutMargins can reduce repeated constraint constants:

swift
1import UIKit
2
3let container = UIView()
4container.translatesAutoresizingMaskIntoConstraints = false
5container.directionalLayoutMargins = NSDirectionalEdgeInsets(
6    top: 12,
7    leading: 20,
8    bottom: 12,
9    trailing: 20
10)
11
12let titleLabel = UILabel()
13titleLabel.translatesAutoresizingMaskIntoConstraints = false
14
15container.addSubview(titleLabel)
16
17NSLayoutConstraint.activate([
18    titleLabel.topAnchor.constraint(equalTo: container.layoutMarginsGuide.topAnchor),
19    titleLabel.leadingAnchor.constraint(equalTo: container.layoutMarginsGuide.leadingAnchor),
20    titleLabel.trailingAnchor.constraint(equalTo: container.layoutMarginsGuide.trailingAnchor),
21    titleLabel.bottomAnchor.constraint(equalTo: container.layoutMarginsGuide.bottomAnchor)
22])

This is a good fit for card views, section containers, and reusable composites where "content lives inside these insets" is part of the component design.

Subclass When the Padding Belongs to the Control Itself

Sometimes the padding is not just layout. It is part of the control's visual identity. A padded label is a common example:

swift
1import UIKit
2
3final class PaddedLabel: UILabel {
4    var textInsets = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
5
6    override func drawText(in rect: CGRect) {
7        super.drawText(in: rect.inset(by: textInsets))
8    }
9
10    override var intrinsicContentSize: CGSize {
11        let size = super.intrinsicContentSize
12        return CGSize(
13            width: size.width + textInsets.left + textInsets.right,
14            height: size.height + textInsets.top + textInsets.bottom
15        )
16    }
17}

This is more reusable than repeating the same extra constraints everywhere a padded label appears.

Choose the Right Level of Ownership

A good rule of thumb is:

  • use constraints when padding is local to one layout
  • use layoutMargins when a container defines a reusable content inset area
  • use a subclass when the control itself owns the padding behavior

That keeps responsibilities clear. The same padding rule should not be half in constraints and half in custom drawing unless there is a strong reason.

Common Pitfalls

The biggest mistake is searching for a padding property on UIView. UIKit does not model spacing that way.

Another common issue is using manual frame math when Auto Layout would be simpler and safer across device sizes.

People also set layoutMargins and then accidentally constrain subviews to the raw anchors instead of the margins guide. At that point the margins have no effect.

Finally, avoid subclassing a generic UIView when the real need is only padding inside a specific control such as a UILabel or UITextField.

Summary

  • 'UIView padding is usually implemented with constraints, margins, or a custom subclass.'
  • Auto Layout constraints are the clearest solution for one-off layouts.
  • 'layoutMargins works well for reusable container insets.'
  • Subclass when the padding is part of the control's own rendering and sizing behavior.
  • Keep the padding rule at one clear ownership level so the layout remains predictable.

Course illustration
Course illustration

All Rights Reserved.