swift UIScrollView不滚动

mm9b1k5b  于 2022-12-26  发布在  Swift
关注(0)|答案(2)|浏览(243)

我在滚动视图中有一个容器视图。该容器视图有其他子视图。滚动功能不起作用。请帮助

UIScrollView和ContainerView此处,容器视图位于滚动视图内部,并且滚动视图具有相对于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
}()

视图ID加载

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()
}

以下是视图的约束条件:

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

我确实尝试过调整滚动视图的内容大小和框架。

0lvr5msh

0lvr5msh1#

滚动视图约束设置为自身。它们需要设置为containerView。请检查containerViewConstraints数组。

4c8rllxm

4c8rllxm2#

在containerViewConstraints中,您正在为scrollView设置约束,该约束应该为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)
    ]

将该行代码添加到其中更改上述代码无效。
containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true

相关问题