Home > Software design >  UIScrollView doesn't scroll
UIScrollView doesn't scroll

Time:12-23

I have a container view inside scroll view. The container view has other subviews. The scroll feature is not working. Please help

UIScrollView and ContainerView Here the container view is inside the scrollview and ScrollView has constraint relative to safeAreaLayoutGuide

private let scrollView: UIScrollView = {
    let scrollView = UIScrollView(frame: .zero)
    scrollView.isScrollEnabled = true
    scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    scrollView.backgroundColor = .systemRed
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    return scrollView
}()

let containerView: UIView = {
    let containerView = UIView()
    return containerView
}()

viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .systemBackground
    view.addSubview(scrollView)
    scrollView.addSubview(containerView)
    scrollView.delegate = self
    containerView.addSubview(titleLabel)
    containerView.addSubview(overviewLabel)
    containerView.addSubview(downloadButton)
    containerView.addSubview(webView)
    applyConstraints()
}

Here are the constraints for the views:

func applyConstraints() {
    let scrollViewConstraints = [
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    ]
    let containerViewConstraints = [
        scrollView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        scrollView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)
    ]
    let webViewConstraints = [
        webView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        webView.heightAnchor.constraint(equalToConstant: 400)
    ]
    let titleLabelConstraints = [
        titleLabel.topAnchor.constraint(equalTo: webView.bottomAnchor, constant: 10),
        titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
    ]
    let overviewLabelConstraints = [
        overviewLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
        overviewLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
        overviewLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
    ]
    let downloadButtonConstraints = [
        downloadButton.topAnchor.constraint(equalTo: overviewLabel.bottomAnchor, constant: 10),
        downloadButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        downloadButton.widthAnchor.constraint(equalToConstant: 140),
        downloadButton.heightAnchor.constraint(equalToConstant: 50)

    ]
    NSLayoutConstraint.activate(scrollViewConstraints)
    NSLayoutConstraint.activate(containerViewConstraints)
    NSLayoutConstraint.activate(webViewConstraints)
    NSLayoutConstraint.activate(titleLabelConstraints)
    NSLayoutConstraint.activate(overviewLabelConstraints)
    NSLayoutConstraint.activate(downloadButtonConstraints)
}

I did try adjusting the content size and frame of the scrollview.

CodePudding user response:

Your scrollview constrains are set to itself. They need to be set to the containerView. Check the containerViewConstraints array.

CodePudding user response:

In containerViewContraints you're setting constraints for scrollView which should be set for contrainerView.

let containerViewConstraints = [
        containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
        .trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)
    ]

Add this line code to it changing above code does not work.

containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true

  • Related