UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

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),
        ])
    }

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.