我要把UIStackView放在UIScrollView中。为了更容易,我将制作思维导图。我放入UIScrollView的UIStackView是思维导图的对象。因此,嵌入式UIStackView将出现在UIScrollView的所有区域中。
但是,虽然我已经为UIScrollView指定了UIStackView的位置,但是UIStackView没有出现在UIScrollView中。
我的界面构建器(附加界面构建器图像以了解My View层次结构。):
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var createObject: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let redView = UIView()
redView.backgroundColor = .red
let blueView = UIView()
blueView.backgroundColor = .blue
let stackView = UIStackView(arrangedSubviews: [redView, blueView])
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.center = scrollView.center
scrollView.addSubview(stackView)
}
}
UIView
和UIStackView
都没有固有的尺寸。
因此,您最终会将包含两个视图的大小为0, 0
的堆栈视图添加到滚动视图中,每个视图的大小为0, 0
。
你需要在某处应用一些尺寸。
这将为您提供一个与滚动视图宽度相等的堆栈视图,以及两倍高(因此您有一个“全高”蓝色视图和一个“全高”红色视图):
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let redView = UIView()
redView.backgroundColor = .red
let blueView = UIView()
blueView.backgroundColor = .blue
let stackView = UIStackView(arrangedSubviews: [redView, blueView])
stackView.axis = .vertical
stackView.distribution = .fillEqually
scrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0.0),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0),
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0.0),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0),
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: 0.0),
stackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor, multiplier: 2.0),
])
}
}
编辑:
这是另一个例子。它添加了6个子视图,具有指定的宽度和高度。堆栈视图属性更改为:
.alignment = .center
.distribution = .fill
.spacing = 8
结果:
并向下滚动:
和代码:
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let colors: [UIColor] = [
.red,
.blue,
.green,
.orange,
.yellow,
.cyan,
]
let sizes: [CGSize] = [
CGSize(width: 100, height: 150),
CGSize(width: 250, height: 100),
CGSize(width: 200, height: 120),
CGSize(width: 160, height: 200),
CGSize(width: 220, height: 180),
CGSize(width: 60, height: 80),
]
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .center
stackView.spacing = 8
for (color, size) in zip(colors, sizes) {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.widthAnchor.constraint(equalToConstant: size.width).isActive = true
v.heightAnchor.constraint(equalToConstant: size.height).isActive = true
v.backgroundColor = color
stackView.addArrangedSubview(v)
}
scrollView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0.0),
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0),
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0.0),
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0),
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: 0.0),
// do NOT set a heightAnchor ... let the subviews define the height
// stackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor, multiplier: 2.0),
])
}
}
您还没有将帧设置为stackview。尝试添加
stackView.frame = UIScreen.main.bounds
在addSubview之后。