我在滚动视图中有一个容器视图。该容器视图有其他子视图。滚动功能不起作用。请帮助
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)
}
我确实尝试过调整滚动视图的内容大小和框架。
2条答案
按热度按时间0lvr5msh1#
滚动视图约束设置为自身。它们需要设置为containerView。请检查containerViewConstraints数组。
4c8rllxm2#
在containerViewConstraints中,您正在为
scrollView
设置约束,该约束应该为contrainerView
设置。将该行代码添加到其中更改上述代码无效。
containerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true