How to Add Padding to a UIView in UIKit (Swift)

How to Add Padding to a UIView in UIKit (Swift)

Written by

in

Quick answer: UIView has no padding property because padding is a layout decision, not something the view draws itself. The quick hack is to inset the view’s bounds, but that shrinks the view and gets overwritten by Auto Layout. For anything real, put your content in a subview and inset that frame in layoutSubviews, or use a container pinned to layoutMarginsGuide.

Why this happens

UIView deliberately has no padding property. A view draws into its bounds rectangle and nothing else, so there is no built-in notion of empty space around its content. If you want padding, you either shrink the area the view considers drawable, or you place the actual content inside a smaller rectangle.

The two rectangles that matter are frame and bounds. frame is the view’s position and size in its superview’s coordinate system. bounds is the view’s own coordinate system, and its size always matches frame (ignoring transforms). Changing bounds.size changes frame.size too, because the view is still a single rectangle.

That is why the classic one-liner “works”: you are not adding empty space, you are resizing the view so that its drawing origin is offset. The view gets smaller by the inset amount, and its content shifts. That is fine for a fixed-size view with no Auto Layout, and misleading everywhere else.

How to fix it

1. The one-liner: inset the bounds (accepted answer)

If the view has a fixed frame and you never touch it again, you can fake padding like this:

view.bounds = view.bounds.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))

The equivalent form you will see in older answers uses frame.insetBy(dx:dy:), which only handles symmetric insets:

view.bounds = view.frame.insetBy(dx: 10.0, dy: 10.0)

Two things happen here. First, the view shrinks by 20 points in each dimension, recentered on the same point. Second, the bounds origin moves to (10, 10), so subviews and drawing start further in. The bounds itself is your “padding box” — the content rect.

Use this only when you control the view entirely: no constraints, no sizeToFit(), no later layout pass. As soon as Auto Layout runs, it recalculates frame from constraints, and frame rewrites bounds — your inset is gone.

2. The reliable way: a content subview laid out by hand

For a custom view that owns its children and does not use constraints internally, override layoutSubviews and inset the children’s frames:

final class PaddedView: UIView {
    var padding: UIEdgeInsets = .zero {
        didSet { setNeedsLayout() }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let contentRect = bounds.inset(by: padding)
        for subview in subviews {
            subview.frame = contentRect
        }
    }
}

This survives resizing because the padding is reapplied on every layout pass. Note the call to setNeedsLayout() in the setter — without it, changing padding after the first layout does nothing until something else triggers a pass.

If the padded view holds a single piece of content, giving it a dedicated container is cleaner, because you keep the outer view’s own background and border untouched:

final class PaddedContainerView: UIView {
    let contentView = UIView()

    var padding: UIEdgeInsets = .zero {
        didSet { setNeedsLayout() }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(contentView)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        addSubview(contentView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.frame = bounds.inset(by: padding)
    }
}

3. With Auto Layout: pin to the layout margins guide

When the content uses constraints, do not set frames at all. Instead set the view’s margins and constrain the content to layoutMarginsGuide:

final class PaddedLayoutView: UIView {
    let contentView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        directionalLayoutMargins = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(contentView)
        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            contentView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
        ])
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}

directionalLayoutMargins uses leading/trailing instead of left/right, so the padding flips automatically in right-to-left languages. If you would rather write the insets as constants, just replace each anchor with constraint(equalTo:contentView.topAnchor, constant: 10) and so on.

4. Padded labels and other intrinsic-size views

For UILabel, UIButton text and similar views, padding has to be part of the measured size, otherwise Auto Layout positions the view as if there were no padding and the text ends up clipping. Subclass and adjust both the drawing rect and the intrinsic size:

final class PaddedLabel: UILabel {
    var padding = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12) {
        didSet {
            invalidateIntrinsicContentSize()
            setNeedsDisplay()
        }
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + padding.left + padding.right,
                      height: size.height + padding.top + padding.bottom)
    }
}

One more option for views that draw their own padding inside a larger frame: override alignmentRectInsets so Auto Layout aligns neighbours to the content rect rather than the outer frame.

override var alignmentRectInsets: UIEdgeInsets {
    UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

This changes how the view participates in layout. It does not change what the view draws, so only reach for it when the padding already exists in the frame.

How to prevent it

  • Never set bounds or frame on a view that has active constraints — the next layout pass overwrites both.
  • Prefer a content subview plus constraints over arithmetic on frame; it keeps padding in one place when the view resizes.
  • Use directionalLayoutMargins rather than left/right constants so right-to-left layouts stay correct.
  • Invalidate intrinsic size (invalidateIntrinsicContentSize()) whenever padding changes on a view that measures itself.
  • Remember that on a UIScrollView, bounds.origin is the scroll position — do not use it for padding there.

FAQ

Why does my padding disappear after the first layout pass?

Because Auto Layout sets frame from your constraints, and setting frame recalculates bounds. Any inset you applied manually is replaced. Move the padding into constraints (a container pinned to layoutMarginsGuide) or apply it inside layoutSubviews, which runs after the engine has done its work.

Is layoutMargins the same thing as padding?

Effectively yes, for constraint-based layouts. layoutMargins describes an inset area inside the view, and layoutMarginsGuide exposes it as anchors. Unlike a bounds hack, it is respected by the layout engine and can be inherited by child views.

How do I add padding to a UIButton’s title?

Use UIButton.Configuration and set contentInsets, which takes a UIEdgeInsets. Assign the configuration to the button’s configuration property and the insets become part of the button’s measured size.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.