1
votes

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
}
2
can you try changing fillProportionally to .fill? and see if it works? - mfaani
Add a contentView inside your scrollview and constraint it to be the height desired. Set the width to >= stackview.widthAnchor. Add all subviews to the scrollView. This will allow the scrollView to size based on its contents and the stackview will constrain it properly. - Brandon
@Honey, i tried, but nothing changed - Bogdan Markov
Sorry. I don't know then. - mfaani

2 Answers

1
votes

If you are targeting iOS 11 or above you can use your UIScrollView's contentLayoutGuide and frameLayoutGuide to distinguish between when you want to constrain the content of your scroll view or the frame of your scroll view. For example here:

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

For example you might try:

scrollView.frameLayoutGuide.heightAnchor.constraint(equalToConstant: 100).isActive = true
scrollView.frameLayoutGuide.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true

You are adding constraints that more aptly apply to your scroll view's frame. And here:

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

you are adding constraints that would more aptly dictate the content size of your scroll view.

For example you might try:

imageStack.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 0).isActive = true
imageStack.leftAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leftAnchor, constant: 0).isActive = true
imageStack.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: 0).isActive = true
imageStack.rightAnchor.constraint(equalTo: scrollView.contentLayoutGuide.rightAnchor, constant: 0).isActive = true

That being said, you are adding more constraints than are strictly necessary, which, in general, is bad and can lead to layout conflicts. If you are adding an arranged subview to a UIStackView you should really only add sizing constraints, as the positioning will be determined by a combination of axis, alignment, distribution, and spacing. Conversely, with your constraints for imageStack, when you are pinning four edges, you don't also need to add sizing constraints, as that will be determined by the pin constraints.

Also, mind the console logs layout errors. If you have any, you might try wtf autolayout to make them more readable.

0
votes

it happened because i added imageStack to vertical stackView twice:

//here i did it first time 
//(part of addMultiFileField method you can see above, in first post)
self.dynamicView.addMultiFileField(forProperty: property)
let scrollView = self.dynamicView.subviews.first(where: { $0 is UIScrollView } ) as! UIScrollView
let stackView = scrollView.subviews.first(where: { $0 is UIStackView }) as! UIStackView
let addButton = stackView.subviews.first(where: { $0 is UIButton } ) as! UIButton
addButton.isUserInteractionEnabled = true
self.oneFileOnly = false
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.addFiles))
addButton.addGestureRecognizer(tapRecognizer)

//and here i did it second time. this line stay here from previous realization and i forgot comment it
self.dynamicView.addArrangedSubview(stackView)

finally i deleted last line and now scroll work fine

Thanks everybody for help!