【发布时间】:2018-01-23 14:29:44
【问题描述】:
我创建了垂直stackView: screenshot
并在其中添加scrollView:
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentMode = .scaleToFill
scrollView.clipsToBounds = true
self.addArrangedSubview(scrollView)
scrollView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
scrollView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 0).isActive = true
scrollView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
scrollView.heightAnchor.constraint(equalToConstant: 100).isActive = true
在这个scrollView里面我添加了水平scrollView:
let imageStack = UIStackView()
imageStack.spacing = 5
imageStack.axis = .horizontal
imageStack.alignment = .center
imageStack.contentMode = .left
imageStack.distribution = .fillProportionally
imageStack.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageStack)
imageStack.heightAnchor.constraint(equalToConstant: 100).isActive = true
imageStack.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
imageStack.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 0).isActive = true
imageStack.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
imageStack.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: 0).isActive = true
在这个 imageStack 中,我以编程方式插入图像。但是当有很多图像(超过 4 个)时,它们会相互叠加并且滚动不起作用。
是否可以在垂直 stackView 内使用水平滚动来排列子视图?或者子视图的宽度不可能超过垂直stackView的宽度?
已编辑
我尝试编辑约束条件,例如 @beyowulf 推荐,但没有任何改变。看看screenshot。我在 imageStack 中添加了 5 张图片,但如果它们和滚动不起作用,则只有部分可见
已编辑 查看我找到 imageStack 的代码并将图像添加到其中:
let scrollView = self.dynamicView.subviews.first(where: {$0 is UIScrollView}) as! UIScrollView
let filesView = scrollView.subviews.first(where: { $0 is UIStackView } ) as! UIStackView
var filesCount = filesView.arrangedSubviews.count - 1
for image in images {
let imgView = UIImageView()
imgView.translatesAutoresizingMaskIntoConstraints = false
let imageViewWidthConstraint = NSLayoutConstraint(item: imgView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
let imageViewHeightConstraint = NSLayoutConstraint(item: imgView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
imgView.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint])
imgView.contentMode = .scaleAspectFill
imgView.clipsToBounds = true
imgView.image = image
imgView.isUserInteractionEnabled = true
let swipeToTopRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(imageDelete))
swipeToTopRecognizer.direction = .up
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
imgView.addGestureRecognizer(tapRecognizer)
imgView.addGestureRecognizer(swipeToTopRecognizer)
filesView.insertArrangedSubview(imgView, at: filesCount)
filesCount += 1
}
【问题讨论】:
-
您可以尝试将
fillProportionally更改为.fill吗?看看它是否有效? -
在滚动视图中添加一个 contentView 并将其限制为所需的高度。将宽度设置为 >= stackview.widthAnchor。将所有子视图添加到滚动视图。这将允许滚动视图根据其内容调整大小,并且堆栈视图将正确约束它。
-
@Honey,我试过了,但没有任何改变
-
对不起。那我不知道。