我想在scrollView中制作一個stackView。 在stackView中,我想添加一些另一個stackViews,但它沒有作業。
所以,為了簡化,我用addArrangedSubview()添加了一些UIViews,但它并沒有顯示任何東西。
我怎樣才能解決這個問題呢?我花了很多時間......
scrollView = UIScrollView()
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activated([
scrollView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor)。
scrollView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor)。
scrollView.centerXAnchor.constraint(等于:view.safeAreaLayoutGuide.centerXAnchor)。
scrollView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor)
])
var line1 = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 1000)
line1.backgroundColor = .blue
var line2 = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 1000)
line2.backgroundColor = .green
剖面圖()
stackView = UIStackView()
stackView.axe = .vertical
stackView.分布 = .fill
stackView.alignment = .fill
stackView.spacing = 10
scrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activated([
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor)。
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor)。
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)。
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
])
stackView.addArrangedSubview(line1)
stackView.addArrangedSubview(line2)
stackView.updateConstraints()
stackView.setNeedsLayout()
uj5u.com熱心網友回復:
我猜在這段代碼運行的時候,ViewControllers - view還沒有設定,所以你的line1/2的寬度會導致0。 你也應該使用自動布局來布局你的視圖。
var line1 = UIView()
line1.translatesAutoresizingMaskIntoConstraints = false
line1.backgroundColor = .blue
var line2 = UIView()
line1.translatesAutoresizingMaskIntoConstraints = false
line2.backgroundColor = .green
stackView = UIStackView( arrangedSubviews: [line1, line2])
stackView.axis = .vertical
stackView.分布 = .fill
stackView.alignment = .fill
stackView.spacing = 10
scrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activated([
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor)。
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor)。
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)。
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)。
line1.widthAnchor.constraint(equalTo: view.widthAnchor)。
line1.heightAnchor.constraint(equalToConstant: 1000)。
line2.widthAnchor.constraint(equalTo: view.widthAnchor)。
line2.heightAnchor.constraint(equalToConstant: 1000)。
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/307960.html
標籤:
