UIScrollView
iOS
Content Layout
Frame Layout
Programming Tips

Remove Content and Frame layout guides from UIScrollview

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

UIScrollView exposes contentLayoutGuide and frameLayoutGuide so Auto Layout can describe both the visible viewport and the full scrollable area. If you are asking how to remove those guides, the practical answer is that you do not remove them; you either constrain against them correctly or choose a manual layout approach that does not depend on them.

What the Layout Guides Actually Mean

The two guides solve different problems. frameLayoutGuide matches the scroll view's visible rectangle. contentLayoutGuide represents the scrollable content region whose size determines whether scrolling is needed.

In modern UIKit, the most reliable pattern is to anchor a single container view to the content guide and then constrain that container's width or height to the frame guide depending on the scroll direction. That gives Auto Layout enough information to derive contentSize for you.

swift
1final class DetailsViewController: UIViewController {
2    private let scrollView = UIScrollView()
3    private let stackView = UIStackView()
4
5    override func viewDidLoad() {
6        super.viewDidLoad()
7
8        view.backgroundColor = .systemBackground
9        scrollView.translatesAutoresizingMaskIntoConstraints = false
10        stackView.translatesAutoresizingMaskIntoConstraints = false
11        stackView.axis = .vertical
12        stackView.spacing = 16
13
14        for index in 1...20 {
15            let label = UILabel()
16            label.numberOfLines = 0
17            label.text = "Row \\(index): Scroll content managed by Auto Layout."
18            stackView.addArrangedSubview(label)
19        }
20
21        view.addSubview(scrollView)
22        scrollView.addSubview(stackView)
23
24        NSLayoutConstraint.activate([
25            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
26            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
27            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
28            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
29
30            stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 20),
31            stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 20),
32            stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -20),
33            stackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -20),
34
35            stackView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor, constant: -40)
36        ])
37    }
38}

That last width constraint is the part many layouts miss. It tells Auto Layout that the content should match the visible width, which enables vertical scrolling without accidental horizontal expansion.

Can You Remove the Guides

No. They are built-in layout guides on the scroll view, not optional helper views you can delete. If a layout behaves badly, the fix is almost always to stop creating conflicting constraints rather than trying to strip the guides out of the hierarchy.

If you genuinely do not want guide-driven Auto Layout, use manual frames and set contentSize yourself. That is a valid choice for simple or highly custom interfaces.

swift
1final class ManualScrollViewController: UIViewController {
2    private let scrollView = UIScrollView()
3    private let imageView = UIImageView(image: UIImage(named: "poster"))
4
5    override func viewDidLoad() {
6        super.viewDidLoad()
7
8        scrollView.frame = view.bounds
9        scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
10        imageView.contentMode = .scaleAspectFit
11
12        scrollView.addSubview(imageView)
13        view.addSubview(scrollView)
14    }
15
16    override func viewDidLayoutSubviews() {
17        super.viewDidLayoutSubviews()
18
19        let width = view.bounds.width
20        imageView.frame = CGRect(x: 0, y: 0, width: width, height: 1200)
21        scrollView.contentSize = imageView.bounds.size
22    }
23}

This version ignores the guides in practice because it does not use Auto Layout for the scroll content. That is different from removing them, and the distinction matters when you are debugging.

When Scroll View Constraints Go Wrong

Most complaints about these guides come from one of three mistakes. First, content is pinned directly to the scroll view's own anchors instead of the content guide, so Auto Layout cannot infer the scrolling region correctly. Second, the content view is pinned on all four sides but is missing a width or height relationship to the frame guide, which leaves the engine with an ambiguous dimension. Third, developers mix manual contentSize changes with Auto Layout constraints, causing the system to fight itself.

A useful rule is simple: pick one ownership model. Either Auto Layout owns contentSize through the guides, or your code owns it through frames and explicit sizing. Mixing both approaches usually creates warnings, jumpy offsets, or content that never scrolls.

Common Pitfalls

Trying to delete contentLayoutGuide or frameLayoutGuide does not work because those guides are part of the UIScrollView API. Rework constraints instead.

Pinning subviews to the scroll view's edges rather than the content guide often produces ambiguous scrolling behavior. Anchor scrollable content to contentLayoutGuide.

Forgetting the width or height tie to frameLayoutGuide is a common source of broken layouts. Add that constraint based on whether scrolling should be vertical or horizontal.

Setting contentSize manually while also relying on guide-based constraints usually creates conflicting behavior. Choose one system and stay consistent.

Summary

  • 'contentLayoutGuide defines the scrollable region, while frameLayoutGuide defines the visible viewport.'
  • You do not remove these guides from a UIScrollView; you either use them correctly or avoid Auto Layout for that part of the interface.
  • For Auto Layout, constrain a container view to the content guide and relate one dimension to the frame guide.
  • For manual layouts, set frames and contentSize yourself and do not expect the guides to manage scrolling.

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.