我将把UIStackView放在UIScrollView中。为了简单起见,我将制作一个思维导图。我放入UIScrollView的UIStackView是思维导图的对象。因此,嵌入式UIStackView将出现在UIScrollView的所有区域中。
但是,尽管我为UIScrollView指定了UIStackView的位置,但是UIStackView不会出现在UIScrollView中。
我的界面构建器(附加界面构建器图像以了解“我的视图”层次结构。):
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)
}
}
答案 0 :(得分:0)
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),
])
}
}
答案 1 :(得分:0)
您尚未将框架设置为stackview。尝试添加
stackView.frame = UIScreen.main.bounds
在addSubview之后。