iOS Development
Screen Dimensions
Swift Programming
UIKit
Mobile App Development

How to get the screen width and height in iOS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On iOS, getting screen width and height is easy, but choosing the correct measurement depends on what you actually need. UIScreen.main.bounds gives you the full screen size in points, while many layout tasks are better served by a view’s bounds or safe area instead of the raw physical screen dimensions.

Get the Full Screen Size in Points

If you want the dimensions of the main display, use UIScreen.main.bounds.

swift
1import UIKit
2
3let screenBounds = UIScreen.main.bounds
4let screenWidth = screenBounds.width
5let screenHeight = screenBounds.height
6
7print(screenWidth)
8print(screenHeight)

These values are in points, not pixels. That is the coordinate system UIKit layout works with by default.

Understand Points Versus Pixels

Developers often expect physical pixels, but iOS layout uses logical points. If you need pixel dimensions, multiply by the screen scale.

swift
1import UIKit
2
3let bounds = UIScreen.main.bounds
4let scale = UIScreen.main.scale
5
6let pixelWidth = bounds.width * scale
7let pixelHeight = bounds.height * scale
8
9print(pixelWidth)
10print(pixelHeight)

Use this only when pixel counts are truly required, such as image processing or low-level rendering decisions.

Use View Bounds for Actual UI Layout

A common mistake is using full screen dimensions to size a view that lives inside a navigation controller, split view, sheet, or other container. In those cases, the relevant size is usually the current view’s bounds.

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLayoutSubviews() {
5        super.viewDidLayoutSubviews()
6        print(view.bounds.width)
7        print(view.bounds.height)
8    }
9}

For layout work, view.bounds is often more correct than UIScreen.main.bounds because it reflects the space your controller actually owns.

Account for Safe Area Insets

Modern devices have notches, home indicators, and other edge constraints. If content must fit within the visible safe region, inspect the safe area instead of only the raw screen rectangle.

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLayoutSubviews() {
5        super.viewDidLayoutSubviews()
6        let safeFrame = view.safeAreaLayoutGuide.layoutFrame
7        print(safeFrame.width)
8        print(safeFrame.height)
9    }
10}

That gives the usable content region after system insets are applied.

Orientation and Window Size Can Change

Screen-related values are not always static in the way older tutorials suggest. Rotation, split view, Stage Manager, and modal presentations can all change the usable size of your interface.

That is why layout code should usually live in lifecycle points such as viewDidLayoutSubviews, Auto Layout constraints, or size-class driven updates, not in one early startup calculation.

If you are building adaptive UI, ask for the size when you need it rather than assuming the first value will remain correct forever.

Prefer Layout Systems Over Manual Screen Math

In many cases, directly reading width and height is not the best design. Auto Layout, stack views, and size classes can express intent more clearly and adapt automatically across devices.

Reading screen dimensions is fine for diagnostics, proportional calculations, or custom drawing. It becomes risky when it turns into hardcoded layout logic that ignores container behavior.

Common Pitfalls

  • Using UIScreen.main.bounds for layout when the real requirement is the current view size.
  • Confusing points with pixels and applying the wrong unit in rendering code.
  • Ignoring safe area insets on modern device layouts.
  • Assuming screen-related measurements never change after initial load.
  • Replacing Auto Layout with manual screen math where the framework already solves the problem better.

Summary

  • Use UIScreen.main.bounds to get full display size in points.
  • Multiply by UIScreen.main.scale only when pixel dimensions are actually needed.
  • Use view.bounds for most real interface layout decisions.
  • Check the safe area when visible content space matters.
  • Prefer adaptive layout systems over hardcoded screen-size calculations.

Course illustration
Course illustration

All Rights Reserved.