I created vertical stackView: screenshot
And add scrollView inside it:
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
Inside this scrollView i added horizontal 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
in this imageStack i programmatically insert images. But when there are many images (more than 4) they are superimposed on each other and scroll is not working.
is it possible to use horizontal scroll for arranged subview inside vertical stackView? or it is impossible to do width of subview more than vertical stackView width?
EDITED
i tried to edit constraints such as @beyowulf recommend, but nothing changed. Look at the screenshot. I added 5 images to imageStack, but visible only part if them and scroll isn`t working
EDITED look at code where i find imageStack and add images into it:
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
}
fillProportionallyto.fill? and see if it works? - mfaani