我的滚动视图不会滚动。我打开了一张类似的票,并被告知要查看 在 Swift 3 中以编程方式使用 ScrollView,但这并没有回答我的问题,所以我再次打开这张票。有人可以调查一下这个问题并且不要关闭这张票吗?我真的需要帮助。我按照另一张票所说的那样设置了内容大小,但这没有做任何事情。请帮忙!
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.bounces = true
scrollView.alwaysBounceVertical = true
scrollView.isScrollEnabled = true
self.view.addSubview(scrollView)
// always a good idea to respect safe area
let safeG = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: safeG.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: safeG.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: 20),
scrollView.bottomAnchor.constraint(equalTo: safeG.bottomAnchor)
])
// Set the content size of the scrollView
scrollView.contentSize = CGSize(width: self.view.frame.width, height: CGFloat(4000))
// Create the UILabels, UIImageView, and UIButton
let titleLabel = UILabel()
titleLabel.text = "Your Intelligence Types"
titleLabel.font = UIFont(name: "Inter-Bold", size: 20)
titleLabel.textAlignment = .center
titleLabel.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(titleLabel)
// Set constraints for the stackView
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor, constant: 20),
titleLabel.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20),
titleLabel.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: -20),
titleLabel.heightAnchor.constraint(equalToConstant: 30)
])
// Create the UILabels, UIImageView, and UIButton
let descriptionLbl = UILabel()
descriptionLbl.text = "Different types of intelligence highlight the diverse ways people think, solve problems, and interact with the world around them."
descriptionLbl.font = UIFont(name: "Inter-Regular", size: 14)
descriptionLbl.textAlignment = .left
descriptionLbl.numberOfLines = 0
descriptionLbl.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(descriptionLbl)
// Set constraints for the stackView
NSLayoutConstraint.activate([
descriptionLbl.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
descriptionLbl.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20),
descriptionLbl.widthAnchor.constraint(equalToConstant: 350),
descriptionLbl.heightAnchor.constraint(equalToConstant: 60)
])
var previousProgressView : UIProgressView? = nil
let previousTitleHeight: CGFloat = 30
let progressViewHeight: CGFloat = 20
let labelHeight: CGFloat = 20
let verticalSpacing: CGFloat = 20
var n = 0;
for (key, value) in totalTypes {
let typeLabel = UILabel()
typeLabel.text = key
typeLabel.font = UIFont(name: "Inter-Bold", size: 16)
typeLabel.textAlignment = .left
typeLabel.translatesAutoresizingMaskIntoConstraints = false
let iprogressView = UIProgressView(progressViewStyle: .bar)
iprogressView.trackTintColor = UIColor(red: 0.07, green: 0.45, blue: 0.87, alpha: 0.1)
iprogressView.tintColor = UIColor(red: 0.07, green: 0.45, blue: 0.87, alpha: 1.00)
iprogressView.layer.cornerRadius = 4
iprogressView.clipsToBounds = true
iprogressView.setProgress(Float(userTypes[key] ?? 0) / Float(value), animated: true)
iprogressView.translatesAutoresizingMaskIntoConstraints = false
let transform : CGAffineTransform = CGAffineTransform(scaleX: 1.0, y: 3.0)
iprogressView.transform = transform
let progressLabel = UILabel()
let progressValue = round(((Float(userTypes[key] ?? 0) / Float(value)) * 100) * 100) / 100
progressLabel.text = "\(progressValue) %"
progressLabel.translatesAutoresizingMaskIntoConstraints = false
progressLabel.font = UIFont(name: "Inter-Regular", size: 16)
scrollView.addSubview(typeLabel)
scrollView.addSubview(iprogressView)
scrollView.addSubview(progressLabel)
NSLayoutConstraint.activate([
typeLabel.topAnchor.constraint(equalTo: previousProgressView?.bottomAnchor ?? descriptionLbl.bottomAnchor, constant: previousProgressView == nil ? verticalSpacing : verticalSpacing),
typeLabel.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20),
typeLabel.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: -20),
typeLabel.heightAnchor.constraint(equalToConstant: 20),
iprogressView.topAnchor.constraint(equalTo: typeLabel.bottomAnchor, constant: 20),
iprogressView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 20),
iprogressView.widthAnchor.constraint(equalToConstant: 200),
iprogressView.heightAnchor.constraint(equalToConstant: 3),
// Position the label next to the progressView
progressLabel.leadingAnchor.constraint(equalTo: iprogressView.trailingAnchor, constant: 10),
progressLabel.centerYAnchor.constraint(equalTo: iprogressView.centerYAnchor)
])
// Update the previousProgressView reference
previousProgressView = iprogressView
n += 1
}
在您的情况下,滚动视图需要来自 safeAreaLayoutGuide 的顶部锚点。
例如
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)