我有一个UIScrollView在我的UIViewController。我可以滚动ScrollView(我可以看到ScrollView底部的栏移动),但我作为子视图放入ScrollView的内容不移动。有人知道我哪里做错了吗?
这是我的ScrollView:
let scrollView = UIScrollView(frame: UIScreen.main.bounds)
这是我在viewDidLoad()中的调用方式:
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize = CGSize(width:2000, height: 5678)
scrollView.isScrollEnabled = true
self.view.addSubview(scrollView)
scrollView.anchor()
scrollView.backgroundColor = .red
let priceLabelStack = UIStackView(arrangedSubviews: [price1Stack, price2Stack, price3Stack, price4Stack])
priceLabelStack.axis = .horizontal
priceLabelStack.spacing = 30
scrollView.addSubview(priceLabelStack)
priceLabelStack.anchor(top: stack.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 20, paddingLeft: 30, paddingRight: 30)
}
这是LayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
}
所以内容(priceLabelStack)不移动…
priceLabelStack.translatesAutoresizingMaskIntoConstraints = false
priceLabelStack.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
priceLabelStack.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
priceLabelStack.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
priceLabelStack.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
这在我的viewDidLoad()
中缺失。现在一切正常