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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.