UIButton
titleLabel
iOS Development
UILabel positioning
Swift Programming

Is it possible to adjust x,y position for titleLabel of UIButton?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

You can influence the position of a button's title, but the right tool is usually not setting titleLabel.frame directly. UIButton manages its internal subviews during layout, so the stable approaches are titleEdgeInsets, contentEdgeInsets, button configuration, or a subclass that overrides layout behavior.

Why Direct Frame Changes Usually Fail

A UIButton owns its titleLabel and updates it whenever layout runs. That means code like this is fragile:

swift
button.titleLabel?.frame.origin.x += 10

It may appear to work briefly, then get overwritten on the next layout pass, state change, or Auto Layout update.

So the engineering rule is simple: do not treat titleLabel like an ordinary subview whose frame you control directly.

Use titleEdgeInsets for Simple Offsets

If you want to nudge the title relative to its default position, titleEdgeInsets is the classic solution.

swift
let button = UIButton(type: .system)
button.setTitle("Buy", for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: -10)

This moves the title to the right by increasing the left inset. For vertical adjustment:

swift
button.titleEdgeInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

The values are relative adjustments, not absolute coordinates. That is why they work best for small layout tweaks rather than pixel-perfect custom placement.

Understand contentEdgeInsets Versus titleEdgeInsets

These two properties solve different problems:

  • 'contentEdgeInsets moves the entire button content area'
  • 'titleEdgeInsets moves the title relative to the content area'
  • 'imageEdgeInsets moves the image relative to the content area'

If the title and image are both off, fix the overall content layout first with contentEdgeInsets, then fine-tune title placement.

swift
button.contentEdgeInsets = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 6, bottom: 0, right: -6)

This tends to be easier to reason about than using one inset property to solve every layout issue.

Modern iOS: Prefer UIButton.Configuration When Appropriate

On newer iOS versions, UIButton.Configuration is often a better API than older manual inset tweaking.

swift
1var config = UIButton.Configuration.plain()
2config.title = "Buy"
3config.image = UIImage(systemName: "cart")
4config.imagePadding = 8
5config.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12)
6
7let button = UIButton(configuration: config)

This does not give arbitrary x and y coordinates for the title label, but it produces more predictable button layouts and is usually the right long-term API for modern UIKit code.

Subclass When You Need Full Custom Layout

If the title truly needs custom positioning beyond simple insets, subclass the button and control layout there.

swift
1class OffsetTitleButton: UIButton {
2    override func layoutSubviews() {
3        super.layoutSubviews()
4        guard let titleLabel = titleLabel else { return }
5        titleLabel.frame.origin.x += 10
6        titleLabel.frame.origin.y += 4
7    }
8}

This gives you stronger control, but it also means you are taking responsibility for state changes, image layout, right-to-left layout behavior, and future UIKit adjustments. Use this only when insets or configuration cannot express the design.

For many designs, placing a UILabel and UIImageView inside a custom UIControl is actually simpler than fighting UIButton layout rules.

Auto Layout Still Matters

If the button itself is constrained by Auto Layout, remember that the button frame can change after initialization. That is another reason layout-dependent adjustments belong in layout code or stable inset properties rather than in one-time setup that assumes a final frame too early.

This is especially relevant when the same button appears in different size classes or dynamic type settings.

Common Pitfalls

  • Setting titleLabel.frame directly and expecting the change to survive layout updates.
  • Using titleEdgeInsets for large absolute positioning changes that really call for a custom control.
  • Forgetting that contentEdgeInsets and titleEdgeInsets solve different layout problems.
  • Tweaking offsets before Auto Layout has produced the button's final size.
  • Ignoring modern UIButton.Configuration when working on newer UIKit code.

Summary

  • You can adjust a button title position, but direct titleLabel.frame changes are fragile.
  • Use titleEdgeInsets for small title-specific shifts.
  • Use contentEdgeInsets to move the whole content area.
  • Prefer UIButton.Configuration for modern UIKit layout where possible.
  • If you need true custom positioning, subclass carefully or build a custom control instead.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.