我添加了一些約束,但它水平滾動,問題出在哪里我如何管理它垂直滾動?我嘗試分別為 contentSize 的寬度和高度添加一些額外的點,但沒有結果它保持水平滾動。
- 我剛剛意識到第二個問題,在我添加 ScrollView 后,我在 UIView 中的收藏按鈕開始不起作用,但是,按鈕添加到 UIView 中的 ContentView 子視圖,它位于 ScrollView 中。提前致謝!
private func setupScrollView() {
self.view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height 1000)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo:self.view.leadingAnchor)
])
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
])
uj5u.com熱心網友回復:
您需要在中設定height的:contentUIScrollViewviewWillLayoutSubviews
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.scrollView.contentSize.height = self.view.frame.height 1000
}
并洗掉這一行:scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height 1000)
uj5u.com熱心網友回復:
你可以試試
解決的關鍵
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height 1000)
}
class ViewController: UIViewController {
let scrollView = UIScrollView()
let contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupScrollView()
}
private func setupScrollView() {
scrollView.backgroundColor = .orange
contentView.backgroundColor = .red
self.view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo:self.view.leadingAnchor),
contentView.widthAnchor.constraint(equalTo: view.widthAnchor),
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.heightAnchor.constraint(equalToConstant: 300)
])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height 1000)
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494326.html
標籤:IOS 迅速 uiscrollview 自动布局 uikit
上一篇:如何用這個json制作結構?
