Left-align image and center text on UIButton
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting a UIButton to show a leading image while keeping the title visually centered is trickier than it first appears. The default button layout treats the image and title as one combined content block, so if you need independent positioning, you usually have to customize layout instead of relying on one alignment property.
Why the Default Layout Is Not Enough
With a stock button, setting contentHorizontalAlignment = .left moves both the image and the title left together. That does not keep the title centered across the button.
If you truly want the image anchored near the leading edge and the title centered within the remaining visual space, you need to control the subview frames or use carefully chosen insets.
A Custom Button Subclass
A simple subclass gives predictable behavior.
This keeps the image near the left edge while centering the title inside the remaining horizontal space.
Using the Custom Button
Because the layout is explicit, the behavior stays stable across different title lengths and image sizes as long as the padding values make sense for the design.
When Insets Are Enough
If the requirement is less strict and you only want the content to appear more balanced, you can sometimes get away with content and edge insets. That is a lighter solution, but it is harder to make truly centered text survive dynamic type, localization, and image-size changes.
For simple fixed-width buttons, insets may be acceptable. For reusable components, custom layout is usually more reliable.
Things to Watch in Real Apps
Buttons with localized titles, right-to-left languages, or large content-size categories need extra testing. A hard-coded layout that looks correct in one language can clip or drift in another.
That does not mean the custom approach is wrong. It means the layout values should be treated as part of the component design and tested under realistic UI conditions.
Interface Builder Versus Code
If the button is created in Interface Builder, keep the visual styling there if you want, but put the layout logic in code so it stays deterministic. Alignment rules that depend on image width, title length, and padding are much easier to maintain when they live in one custom subclass instead of being split across storyboard tweaks and runtime adjustments.
Common Pitfalls
- Using
contentHorizontalAlignment = .leftand expecting the title to stay centered independently of the image. - Relying only on edge insets for a layout that really requires custom positioning logic.
- Forgetting to test longer localized titles and larger accessibility text sizes.
- Positioning the title across the full button width and letting it overlap the image.
- Assuming
UIButton's default content layout is designed for independently aligned image and text.
Summary
- A default
UIButtontreats image and title as one content block. - For a true left-image, centered-title layout, custom positioning is usually the cleanest solution.
- A subclass with
layoutSubviews()gives predictable control. - Insets can work for simpler fixed designs, but they are less robust.
- Test the button with localization and dynamic type before treating the layout as finished.
Related reading
- Left Align Cells in UICollectionView
- Lesser than or greater than in Swift switch statement
- Libraries do not get added to APK anymore after upgrade to ADT 22
- Libraries not found when using CocoaPods with iOS logic tests
- library not found for -lPods
- Limit number of characters in uitextview
- Limitation with classes derived from generic classes in swift
- Linear Layout and weight in Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.