[英]ScrollView with AutoLayout not working
我在UIScrollView上以編程方式添加子視圖很費勁,我看過很多教程,但是它們都集中在故事板上,並不能真正解決我的問題,我只是想在scrollView中添加一些子視圖,但是不會在設備上顯示任何內容,它只顯示scrollView而不顯示我的子視圖,我試圖做這樣的事情:
view.addSubview(mainScrollView)
mainScrollView.anchor(top: view.safeAreaLayoutGuide.topAnchor, right: view.rightAnchor, bottom: view.bottomAnchor, left: view.leftAnchor, topPadding: 10, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 0)
let v1 = UIView()
v1.backgroundColor = .blue
let v2 = UIView()
v2.backgroundColor = .black
let v3 = UIView()
v3.backgroundColor = .yellow
let v4 = UIView()
v4.backgroundColor = .green
mainScrollView.addSubview(v1)
mainScrollView.addSubview(v2)
mainScrollView.addSubview(v3)
mainScrollView.addSubview(v4)
v1.anchor(top: mainScrollView.topAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)
v2.anchor(top: v1.bottomAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)
v3.anchor(top: v2.bottomAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)
v4.anchor(top: v3.bottomAnchor, right: nil, bottom: mainScrollView.bottomAnchor, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)
錨只是一個輔助擴展,可以更輕松地約束和激活約束
1-任何帶有約束的程序視圖
v.translateAutoresizingMaskIntoconstarints = false
2-您沒有給v1 ... 4設置高度限制,請檢查此
import UIKit
class ViewController: UIViewController {
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
let viewsCount = 7
var prevView = self.view!
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor,constant:20),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
for i in 0..<viewsCount {
let myView = UIView()
myView.backgroundColor = (i % 2 == 0 ) ? .red : .green
myView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(myView)
if prevView == self.view {
myView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
}
else {
myView.topAnchor.constraint(equalTo: prevView.bottomAnchor).isActive = true
}
NSLayoutConstraint.activate([
myView.widthAnchor.constraint(equalToConstant: self.view.frame.width),
myView.heightAnchor.constraint(equalToConstant: 400)
])
if i == viewsCount - 1 {
myView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
}
prevView = myView
}
}
}
因為您正在初始化沒有框架的子視圖,所以您的視圖似乎沒有高度,除非anchor函數執行某些操作以賦予其高度。
這是擴展
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, topPadding: CGFloat, rightPadding: CGFloat, bottomPadding: CGFloat, leftPadding: CGFloat, width: CGFloat, height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: topPadding).isActive = true
}
if let right = right {
self.rightAnchor.constraint(equalTo: right, constant: -rightPadding).isActive = true
}
if let bottom = bottom {
self.bottomAnchor.constraint(equalTo: bottom, constant: -bottomPadding).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: leftPadding).isActive = true
}
if width != 0 {
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}
if height != 0 {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.