WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: UIScrollView does not really scroll

Forums > iOS

Hi guys,

As above, my scrollview does not scroll, here is my code:

 private func layoutScrollView() {
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        scrollView.backgroundColor = .systemRed

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint             (equalTo: view.topAnchor),
            scrollView.bottomAnchor.constraint          (equalTo: view.bottomAnchor),
            scrollView.leadingAnchor.constraint         (equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint        (equalTo: view.trailingAnchor),
        ])
    }

    private func layoutUIInScrollView() {
        scrollView.addSubview(hiNameLabel)
        scrollView.addSubview(logOutButton)

        NSLayoutConstraint.activate([
            logOutButton.centerXAnchor.constraint       (equalTo: scrollView.centerXAnchor),
            logOutButton.topAnchor.constraint           (equalTo: scrollView.centerYAnchor),
            logOutButton.widthAnchor.constraint         (equalToConstant: 150),
            logOutButton.heightAnchor.constraint        (equalToConstant: 500),

            hiNameLabel.centerXAnchor.constraint        (equalTo: scrollView.centerXAnchor),
            hiNameLabel.topAnchor.constraint            (equalTo: logOutButton.bottomAnchor),
            hiNameLabel.widthAnchor.constraint          (equalToConstant: 150),
            hiNameLabel.heightAnchor.constraint         (equalToConstant: 500),
        ])
    }

Do I miss any particular element? isScrollEnabled = true does not help as I believe its a default.

1      

Okay I got it.

  1. A scrollView needs to be pinned to the edges of a view.
  2. Create a contentView = UIView()
  3. Pin a contentView to the edgees of a scrollView
  4. Add two extra constraints for a contentView
    1. contentView's width to scrollView's width
    2. contentView's height to fixed to the value that you calculated
 private func layoutScrollViewAndContentView() {
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        contentView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        scrollView.backgroundColor = .systemRed
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint             (equalTo: view.topAnchor),
            scrollView.bottomAnchor.constraint          (equalTo: view.bottomAnchor),
            scrollView.leadingAnchor.constraint         (equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint        (equalTo: view.trailingAnchor),

            contentView.topAnchor.constraint            (equalTo: scrollView.topAnchor),
            contentView.bottomAnchor.constraint         (equalTo: scrollView.bottomAnchor),
            contentView.leadingAnchor.constraint        (equalTo: scrollView.leadingAnchor),
            contentView.trailingAnchor.constraint       (equalTo: scrollView.trailingAnchor),

            contentView.widthAnchor.constraint          (equalTo: scrollView.widthAnchor),
            contentView.heightAnchor.constraint         (equalToConstant: 2000),

        ])
    }

    private func layoutUIInScrollView() {
        contentView.addSubview(hiNameLabel)
        contentView.addSubview(logOutButton)

        NSLayoutConstraint.activate([
            hiNameLabel.centerXAnchor.constraint        (equalTo: contentView.centerXAnchor),
            hiNameLabel.topAnchor.constraint            (equalTo: contentView.topAnchor),
            hiNameLabel.widthAnchor.constraint          (equalToConstant: 150),
            hiNameLabel.heightAnchor.constraint         (equalToConstant: 2000),

            logOutButton.centerXAnchor.constraint       (equalTo: contentView.centerXAnchor),
            logOutButton.topAnchor.constraint           (equalTo: contentView.centerYAnchor),
            logOutButton.widthAnchor.constraint         (equalToConstant: 150),
            logOutButton.heightAnchor.constraint        (equalToConstant: 500),
        ])
    }

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.