How to center UILabel in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To center a UILabel in its superview in UIKit, use Auto Layout constraints to pin its centerXAnchor and centerYAnchor to the superview's corresponding anchors. In Interface Builder, select the label and add "Horizontally in Container" and "Vertically in Container" constraints. In SwiftUI, views are centered by default within their parent container. The key requirement in programmatic UIKit is setting translatesAutoresizingMaskIntoConstraints = false before adding constraints, otherwise the autoresizing mask conflicts with Auto Layout.
Programmatic Auto Layout (UIKit)
translatesAutoresizingMaskIntoConstraints = false tells UIKit to use Auto Layout constraints instead of the legacy autoresizing mask. Without this, the constraints conflict with auto-generated ones and the label does not center correctly.
Centering Within a Specific Area
Centering Text Within the Label
textAlignment controls how text is aligned within the label's bounds. centerXAnchor/centerYAnchor controls where the label itself is positioned within its superview. Both are needed for fully centered text on screen.
Interface Builder / Storyboard
Interface Builder generates the same NSLayoutConstraint code as the programmatic approach. Use the Document Outline panel to verify constraints are attached to the correct views.
Using UIStackView for Centering
UIStackView with .alignment = .center centers all arranged subviews along the cross axis. This is useful when centering multiple labels or a group of views.
SwiftUI Centering
In SwiftUI, Text views are centered within their parent by default. Use .frame(maxWidth: .infinity, maxHeight: .infinity) to expand the view to fill available space and center within it.
Common Pitfalls
- Forgetting
translatesAutoresizingMaskIntoConstraints = false: Without this, the view's autoresizing mask creates conflicting constraints. Auto Layout logs ambiguous layout warnings, and the label does not position correctly. Always set this tofalsefor programmatic constraints. - Adding constraints before adding the subview: Constraints reference the view hierarchy. If you activate constraints before calling
addSubview(), the app crashes with "Unable to activate constraint with anchors...not a common ancestor." Always add the subview first. - Centering text vs centering the label:
textAlignment = .centercenters text within the label's frame. If the label itself is left-aligned in the superview, the text appears off-center on screen. You need bothtextAlignmentand centering constraints. - Not using
safeAreaLayoutGuidefor vertical centering: On devices with notches or home indicators, centering relative toview.centerYAnchormay appear visually off-center because the safe area is not symmetric. Useview.safeAreaLayoutGuide.centerYAnchorfor a visually balanced position. - Conflicting width constraints with centering: Adding both leading/trailing constraints and a centerX constraint can cause conflicts. Use either leading+trailing (for full-width labels) or centerX+width (for fixed-width labels), not both simultaneously.
Summary
- Use
centerXAnchorandcenterYAnchorconstraints to center aUILabelprogrammatically - Always set
translatesAutoresizingMaskIntoConstraints = falsebefore adding constraints - Set
textAlignment = .centerto center text within the label's bounds - In Interface Builder, use "Horizontally in Container" and "Vertically in Container" alignment constraints
- Use
UIStackViewwith.alignment = .centerto center groups of views - In SwiftUI,
Textis centered by default; use.frame(maxWidth: .infinity)for explicit full-space centering
Related reading
- How to change an Android app's name?
- How to change Android version and code version number?
- How to change background color in android app
- How to change button text in Swift Xcode 6?
- How to change button text in Swift Xcode 6?
- How to change Button Title Alignment in Swift?
- How to change color of Android ListView separator line?
- how to change color of textview hyperlink?
.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.