How to get the iPhone's screen width in SwiftUI?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In SwiftUI, asking for "the screen width" is often really a layout question. Sometimes you truly want the device-wide width in points, but more often you want the width actually available to a specific view after containers, safe areas, and multitasking rules are applied. SwiftUI has APIs for both cases, and choosing the right one prevents brittle layouts.
Use GeometryReader for Real Layout Decisions
If the goal is to size or adapt a SwiftUI view, GeometryReader is usually the correct answer because it reports the size offered by the parent container.
This is usually better than reading a global screen metric because it works correctly inside sheets, split views, sidebars, and other containers where the full device width is not actually available.
Use UIScreen Only When You Truly Need Device Width
If you really need the current physical screen width in points, you can still read it through UIKit:
This gives the screen bounds, not the local layout width. That distinction matters. A child view inside a narrow container may have far less room than UIScreen.main.bounds.width suggests.
Screen Width and Available Width Are Not the Same
These values diverge in several common situations:
- split-screen multitasking on iPad
- sheet or popover presentation
- navigation split layouts
- embedded child views
- safe-area constrained content
If your layout logic depends on the space a view can actually use, container width is the meaningful number, not global screen width.
Environment-Driven Layout Can Be Better Than Manual Width Checks
Sometimes you do not need a numeric width value at all. SwiftUI includes adaptive APIs that often express intent more clearly.
A simple example is size class:
You can also use tools such as ViewThatFits, flexible frames, LazyVGrid, and Spacer to let SwiftUI adapt automatically instead of branching on hardcoded width thresholds.
Reading Width in a Reusable Way
If several views need container width, wrap the pattern so the layout code stays tidy:
Usage:
This keeps geometry concerns local and avoids repeating the same setup throughout the view tree.
Common Pitfalls
One common mistake is using UIScreen.main.bounds.width for child-view layout inside a sheet or nested container. That often overestimates the real available width and breaks the design.
Another mistake is assuming the width is fixed. Rotation, multitasking, and dynamic window changes can alter available width during runtime.
Developers also sometimes reach for manual width breakpoints when SwiftUI’s adaptive layout tools would express the intent more directly and with less maintenance.
Finally, be clear about whether you need device width or local container width. Those are different measurements and should not be treated as interchangeable.
Summary
- Use
GeometryReaderwhen you need the width available to a specific SwiftUI view. - Use
UIScreen.main.bounds.widthonly when you truly need the global device screen width. - Container width is usually more useful than raw screen width for layout decisions.
- Size classes and adaptive SwiftUI layout tools often remove the need for manual width checks.
- Most layout bugs come from confusing device metrics with local layout space.
Related reading
- How to get the name of enumeration value in Swift?
- How to get the name of the running application in iOS
- How to get the Power of some Integer in Swift language?
- How to get the result of OnPostExecute to main activity because AsyncTask is a separate class?
- How to get the screen width and height in iOS?
- How to get the scroll bar with CSS overflow on iOS
- How to get the selected index of a RadioGroup in Android
- How to get the selected index of a RadioGroup 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.